代码之家  ›  专栏  ›  技术社区  ›  Roby Sottini

yii2:如何使用jquery使a<div>禁用/只读

  •  3
  • Roby Sottini  · 技术社区  · 8 年前

    我需要禁用或使只读成为 yii2-date-picker-widget 在yii2框架下。

    DatePicker小部件有两部分: <span> 对于图标和 <input> 为了约会。我可以禁用 <输入& GT; 使用jquery但我不能 <SPAN & GT; .

    Datepicker

    这是jquery代码:

    $("#movements-vencimiento").prop("disabled", true); // Works.
    $("#vencimientoDiv").prop("disabled", true) // Not works.
    

    这是yii2代码:

    <div class="col col-md-4" id="vencimientoDiv">
        <?= $form->field($model, 'vencimiento')->widget(\common\widgets\DatePicker::className(), [
            'convertFormat' => true,
            'pluginOptions' => [
                'format' => 'dd/mm/yyyy',
                'autoclose' => true,
                'language' => 'es-AR',
                'todayHighlight' => 'true',
            ]
        ]) ?>
    </div>
    

    这是HTML生成的代码:

    <div id="vencimientoDiv">
        <div class="form-group field-movements-vencimiento">
            <label class="control-label" for="movements-vencimiento">Vencimiento</label>
            <div class="input-group date">
                <span class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                </span>
                <input type="text" id="movements-vencimiento" class="form-control" name="Movements[vencimiento]" disabled="">
            </div>
            <div class="help-block"></div>
        </div>
    </div>
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   Tobias Geiselmann    8 年前

    div没有 disabled 财产。相反,您可以删除导致日历显示的事件处理程序,如下所示:

    $("#vencimientoDiv *").off();
    

    就这么简单。

        2
  •  3
  •   Muhammad Omer Aslam    8 年前

    你要找的是 pointerEvents 它对所有元素都有效,您可以在需要时禁用并启用单击返回。

    查看下面的简单演示

    $("#clicker").on('click', function(e) {
      e.preventDefault();
      alert("clicked");
    });
    
    $("#disab").on('click', function() {
      $("#clicker").css({
        pointerEvents: 'none'
      });
    })
    
    $("#enab").on('click', function() {
      $("#clicker").css({
        pointerEvents: 'auto'
      });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="#." id="clicker">click me</a>
    
    <input type="button" value="Disable Click" id="disab">
    <input type="button" value="enable Click" id="enab">

    因此,在您的情况下,您将写下以下禁用

    $("#vencimientoDiv").css({pointerEvents:'none'});
    

    然后使用

    $("#vencimientoDiv").css({pointerEvents:'auto'});