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

Posted

技术标签:

【中文标题】我如何使用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检查单选按钮是不是被选中[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何检查“单选按钮”是不是被选中?

如何检查单选按钮是不是在 Android 的单选组中被选中?

如何检查单选按钮是不是被选中

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

检查是不是选中了单选按钮jquery [重复]

如何通过检查另一个单选按钮来设置选中的单选按钮