【问题标题】:Toggle inputs readonly explanation切换输入只读说明
【发布时间】:2017-06-28 07:44:55
【问题描述】:

我想在一个 Div 中切换所有输入的只读属性。 我找到了一些解决方案,但只有一个有效。

我想知道为什么这段代码不起作用:

$("#" + id + " :input").each( function() {
       $(this).readOnly = !$(this).readOnly;
});

这是工作代码:

$("#" + id + " :input").each( function() {
    (this).attr("disabled", function(i, d) {
      return !d;   
    });
});

谁能给我解释一下?

【问题讨论】:

    标签: javascript jquery toggle readonly


    【解决方案1】:

    你可以试试这个:Working Fiddle

    我们可以使用 jquery 属性来切换只读属性。

    HTML:

    <input  type="button" id="btnToggle" value="Toggle" />
    <div id="divToggle">
      <label>First:</label><input type="text" id="txtFirst" /><br/>
      <label>Second:</label><input type="text" id="txtSecond" /><br/>
      <label>Third:</label><input type="text" id="txtThird" /><br/>
    </div>
    

    jQuery:

    $("#btnToggle").click(function(){
      var id="divToggle";
      $("#" + id + " :input").each( function() {
          $(this).prop("readOnly",!$(this).prop("readOnly"));
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-22
      • 2020-01-26
      • 2014-06-24
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多