jQuery怎样判断按钮是不是被选中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery怎样判断按钮是不是被选中相关的知识,希望对你有一定的参考价值。

 if($('#xy_checkbox').is(':checked'))
     //如果被选中
 else
    //未选中
 

参考技术A 建议你下一个手册,先看一遍
表单对象属性:

:checked

匹配所有选中的被选中元素(复选框、单选框等,不包括select中的option)

$("input:checked")

我如何使用jquery检查单选按钮是不是被选中[重复]

【中文标题】我如何使用jquery检查单选按钮是不是被选中[重复]【英文标题】:How do i check a radio button is checked or not using jquery [duplicate]我如何使用jquery检查单选按钮是否被选中[重复] 【发布时间】:2017-08-25 17:40:40 【问题描述】:

如何使用 jquery 检查单选按钮是否被选中???

代码如下:

     function handlesClick(myRadio) 
        debugger
       var selectedmerchant = [];
        if (myRadio != '') 
            var myRadios = $('input[name=myRadio]');
            //var checkedValue = $("#myRadio").attr('checked', 'checked');
            //if ($('#myRadio').is(':checked'))  alert("it's checked"); 
            //$('input[type="radio"][class="icheck-list"]:checked').val()
            if ($("#myRadio").is(":checked") === true)
            
                selectedmerchant.push(myradio);
                selectedmerchant.join(",");
            
            else
            
 var index = selectedmerchant.indexof(merchant);
               selectedmerchant.splice(index, 1);
            
     //if (document.getelementbyid(myradio).checked == true) 
            //    selectedmerchant.push(myradio);
            //    selectedmerchant.join(",");
            //
            //else 
            //    var index = selectedmerchant.indexof(merchant);
            //    selectedmerchant.splice(index, 1);
            //

我想检查复选框是否被选中,以便我可以使用 javascript 在数组中推送或切片。 任何代码???

这里是html:

  <div class="row">
    <div class="form-group">
      <div class="col-md-12">
        <label>3.sdfdsfsdfds</label>
        <div class="icheck-list">
          <label class="icheck-list" id="11">

            <input name="3" onclick="handlesClick(this);" id="3_11" type="radio">Strongly Disagree
            <span></span>
          </label>
        </div>
        <div class="icheck-list">
          <label class="icheck-list" id="12">
            <input name="3" onclick="handlesClick(this);" id="3_12" type="radio">Disagree
            <span></span>
          </label>
        </div>
        <div class="icheck-list">
          <label class="icheck-list" id="13">
            <input name="3" onclick="handlesClick(this);" id="3_13" type="radio">Neither Agree or Disagree
            <span></span>
          </label>     
        </div>
        <div class="icheck-list">
          <label class="icheck-list" id="14">
            <input name="3" onclick="handlesClick(this);" id="3_14" type="radio">Agree
            <span></span>
          </label>
        </div>
        <div class="icheck-list">
          <label class="icheck-list" id="15">
            <input name="3" onclick="handlesClick(this);" id="3_15" type="radio">Strongly Agree
            <span></span>
          </label>
        </div>
      </div> 
    </div>
  </div>

【问题讨论】:

不工作这就是我问男人的原因 显示一些相关的html 这可能意味着你的代码没有被正确调用 我已经用html编辑了.. 格式化你的html和js) 【参考方案1】:

检查一下,

window.selectedmerchant = ;
function handlesClick(myRadio) 
  if ($(myRadio).is(":checked")) 
   window.selectedmerchant[$(myRadio).attr("name")] = $(myRadio).attr("id");
  
  console.log(window.selectedmerchant);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="form-group">
    <div class="col-md-12">
      <label>3.sdfdsfsdfds
      </label>
      <div class="icheck-list">
        <label class="icheck-list" id="11">
          <input name="3" onclick="handlesClick(this);" id="3_11" type="radio">Strongly Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="12">
          <input name="3" onclick="handlesClick(this);" id="3_12" type="radio">Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="13">
          <input name="3" onclick="handlesClick(this);" id="3_13" type="radio">Neither Agree or Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="14">
          <input name="3" onclick="handlesClick(this);" id="3_14" type="radio">Agree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="15">
          <input name="3" onclick="handlesClick(this);" id="3_15" type="radio">Strongly Agree
          <span></span>
        </label>
      </div>
    </div>
  </div>
</div>
<hr/>
<div class="row">
  <div class="form-group">
    <div class="col-md-12">
      <label>4. fourth question
      </label>
      <div class="icheck-list">
        <label class="icheck-list" id="13">
          <input name="4" onclick="handlesClick(this);" id="4_13" type="radio">Strongly Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="14">
          <input name="4" onclick="handlesClick(this);" id="4_14" type="radio">Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="15">
          <input name="4" onclick="handlesClick(this);" id="4_15" type="radio">Neither Agree or Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="16">
          <input name="4" onclick="handlesClick(this);" id="4_16" type="radio">Agree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="17">
          <input name="4" onclick="handlesClick(this);" id="4_17" type="radio">Strongly Agree
          <span></span>
        </label>
      </div>
    </div>
  </div>
</div>
<hr/>
<div class="row">
  <div class="form-group">
    <div class="col-md-12">
      <label>5.5th question
      </label>
      <div class="icheck-list">
        <label class="icheck-list" id="18">
          <input name="5" onclick="handlesClick(this);" id="5_18" type="radio">Strongly Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="19">
          <input name="5" onclick="handlesClick(this);" id="5_19" type="radio">Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="20">
          <input name="5" onclick="handlesClick(this);" id="5_20" type="radio">Neither Agree or Disagree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="21">
          <input name="5" onclick="handlesClick(this);" id="5_21" type="radio">Agree
          <span></span>
        </label>
      </div>
      <div class="icheck-list">
        <label class="icheck-list" id="22">
          <input name="5" onclick="handlesClick(this);" id="5_22" type="radio">Strongly Agree
          <span></span>
        </label>
      </div>
    </div>
  </div>
</div>
<hr/>

我只举了三个​​例子作为演示,但检查代码,它现在工作正常。

我希望这会有所帮助。

【讨论】:

【参考方案2】:

为此,请尝试以下代码:

<input name="3" onclick="handlesClick(this);" id="3_1" type="radio">Agree

<input name="3" onclick="handlesClick(this);" id="3_2" type="radio">Disagree

// 这里是JS代码 //

function handlesClick(RadioId) 

if (RadioId != '') 
if ($(RadioId).is(":checked") === true) 
    

  //Add condition for checked Radio

      
 else 
    

   //Add condition for unchecked Radio

    
  

【讨论】:

以上是关于jQuery怎样判断按钮是不是被选中的主要内容,如果未能解决你的问题,请参考以下文章

怎样判断一组checkbox中是不是有被选中的?

怎样判断checkbox是不是选中

jquery怎么判断某一个复选框是不是被选中啊?

JQuery判断radio(单选框)是否选中和获取选中值方法总结

layer.js怎样判断checkbox是不是被选中

WinForm中datagridview里怎样判断checkbox是不是被选中~