【问题标题】:How uncheck others when check one in jQuery Mobile Controlgroup?在 jQuery Mobile Controlgroup 中选中一项时如何取消选中其他人?
【发布时间】:2017-09-16 20:12:04
【问题描述】:

我的目标是在选中其中一个复选框时取消选中其他人。这是不适用于 jquery mobile 1.4.2 的代码 ...

JavaScript:

$('input.answer').on('change', function() {
    $('input.answer').not(this).prop('checked', false);  
});

HTML:

<form>
  <fieldset data-role="controlgroup">
    <legend for="0-0">Uncheck others when check one of the items</legend>
    <label for="0-0-4">one</label>
    <input class="answer answer-num-6" type="checkbox" name="0-0-4" id="0-0-4" value="4">
    <label for="0-0-3">two</label>
    <input class="answer answer-num-6" type="checkbox" name="0-0-3" id="0-0-3" value="3">
    <label for="0-0-2">three</label>
    <input class="answer answer-num-6" type="checkbox" name="0-0-2" id="0-0-2" value="2">
    <label for="0-0-1">four</label>
    <input class="answer answer-num-6" type="checkbox" name="0-0-1" id="0-0-1" value="1">
    <label for="0-0-0">five</label>
    <input class="answer answer-num-6" type="checkbox" name="0-0-0" id="0-0-0" value="0">
  </fieldset>
</form> 

【问题讨论】:

    标签: jquery jquery-mobile checkbox


    【解决方案1】:

    如果您使用的是 jquery UI,只需添加 checkboxradio 功能...

    $('input.answer').not(this).prop('checked', false).checkboxradio("refresh");
    

    祝你有美好的一天

    【讨论】:

    • 虽然你的答案仍然有效,但我只是添加了另一种方法来做到这一点...... :)
    【解决方案2】:

    因为 jQuery Mobile 将自己的样式添加到每个 Checkbox - 以及整个 Controlgroup - 您应该调用包含您更改的元素的 Controlgrouprefresh 方法。请注意,您应该在 pagecreate 事件中添加您的事件监听器。

    $(document).on("pagecreate", "#page-one", function() {
      $('input.answer').on('change', function() {
        
        $('input.answer').not(this).prop('checked', false);
        $(this).closest("fieldset").controlgroup("refresh"); // add this line
        
        /* Display the result */
        var result = "";
        $("input.answer").each(function(index) {
          var caption = $(this).parent().find("label").text();
          var checked = $(this).prop('checked');
          result += caption + ":" + checked + "  ";
        });
        $(this).closest("fieldset").find("legend").html(result);
        
      });
    });
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
      <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
      <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
      <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
    </head>
    <body>
      <div id="page-one" data-role="page">
        <div role="main" class="ui-content">
          <form>
            <fieldset data-role="controlgroup" data-mini="true">
              <legend for="0-0">Uncheck others when check one of the items</legend>
              <label for="0-0-4">one</label>
              <input class="answer answer-num-6" type="checkbox" name="0-0-4" id="0-0-4" value="4">
              <label for="0-0-3">two</label>
              <input class="answer answer-num-6" type="checkbox" name="0-0-3" id="0-0-3" value="3">
              <label for="0-0-2">three</label>
              <input class="answer answer-num-6" type="checkbox" name="0-0-2" id="0-0-2" value="2">
              <label for="0-0-1">four</label>
              <input class="answer answer-num-6" type="checkbox" name="0-0-1" id="0-0-1" value="1">
              <label for="0-0-0">five</label>
              <input class="answer answer-num-6" type="checkbox" name="0-0-0" id="0-0-0" value="0">
            </fieldset>
          </form>
        </div>
      </div>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多