【问题标题】:jquery is checked on radio buttonjquery 在单选按钮上被选中
【发布时间】:2012-06-28 19:10:02
【问题描述】:

所以,我有 3 个单选按钮:

<input type="radio" name="status" value="enabled" class="radio" <?php if($news_row['status']==1) echo 'checked'; ?> />
<input type="radio" name="status" value="disabled" class="radio" <?php if($news_row['status']==2) echo 'checked'; ?> />
<input type="radio" name="status" value="timer" class="radio" <?php if($news_row['status']==3) echo 'checked'; ?> />

我正在尝试检查是否像这样检查了带有值计时器的单选按钮

if($('.radio [value=timer]').is(':checked')) {
    console.log('timer');
}

但显然我做错了,因为上面的代码不起作用。那么,哪种方法是正确的呢?使用.change()?

【问题讨论】:

    标签: jquery if-statement radio-button checked


    【解决方案1】:

    删除空格

    if($('.radio[value=timer]').is(':checked')) {
        console.log('timer');
    }
    

    .radio [value=timer] 中有空格,这意味着不同的选择器,例如它意味着 select all elements with value attribute set inside an element with class radio at any nested level

    去掉空格表示select all elements with class radio AND value attribute set

    【讨论】:

      【解决方案2】:

      演示 http://jsfiddle.net/m4m57/5/

      问题是space 这里f ($('.radio [value=timer]').is(':checked')) {

                                          ^-----------
      

      希望这会有所帮助,

      代码

      $('input').on('change', function() {
          if ($('.radio[value=timer]').is(':checked')) {
              alert('say what');
              console.log('timer');
          }
      });​
      

      【讨论】:

        猜你喜欢
        • 2012-03-04
        • 2014-04-23
        • 2016-06-09
        • 2019-12-21
        • 2023-02-03
        • 1970-01-01
        • 1970-01-01
        • 2017-10-18
        • 2013-10-02
        相关资源
        最近更新 更多