Materialisecss的复选框输入无法使用jQuery

时间:2016-12-27 01:54:54

标签: javascript jquery css checkbox materialize

我正在构建Simon游戏,我正在使用Materialize.css库中的两组checkbox inputs

如官方docs所示,这里是checkbox -

的摘要
<p>
  <input type="checkbox" id="test5" />
  <label for="test5">Red</label>
</p> 

问题是,当我尝试使用jQuery检测checkboxchecked时,它似乎根本不起作用。我尝试了is(":checked")prop("checked"),但都没有效果。

/**
 * Created by manishgiri on 12/26/16.
 */
$(document).ready(function () {
    if($("#start").is(":checked")) {
        console.log("inside");
        console.log("Start is checked");
    }
    if($("#start").prop("checked")) {
        console.log("inside");
        console.log("Start is checked");
    }
    //console.log("outside");
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>
      <input type="checkbox" id="start" />
      <label for="start">Start</label>
    </p>

这是Codepen上的完整应用。我将不胜感激任何帮助!

1 个答案:

答案 0 :(得分:2)

您的代码在写入时正常运行 - 在Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false, block: { (Timer) in //Upload 1st package print("The 1st package upload!") } Timer.scheduledTimer(withTimeInterval: 20.0, repeats: false, block: { (Timer) in //Upload 2nd package print("The 2nd package upload!") } Timer.scheduledTimer(withTimeInterval: 30.0, repeats: false, block: { (Timer) in //Upload 3rd package print("The 3rd package upload!") } //....And so on.... (DOM加载)上,正在查看复选框以查看是否已选中它。因为代码只运行一次,所以当复选框被更改时,它不会做任何事情。

要获得所需的行为,您需要在输入中使用$(document).ready事件。这是jQuery风格:

&#13;
&#13;
onchange
&#13;
/**
 * Created by manishgiri on 12/26/16.
 */
$(document).ready(function() {
  $('#start').on('change', function() {
    if ($(this).is(':checked')) {
      console.log('Start is checked');
    }
  });
});
&#13;
&#13;
&#13;