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

时间:2017-03-31 12:36:34

标签: javascript jquery arrays asp.net-mvc-4

如何使用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>

2 个答案:

答案 0 :(得分:2)

检查一下,

&#13;
&#13;
window.selectedmerchant = {};
function handlesClick(myRadio) {
  if ($(myRadio).is(":checked")) {
   window.selectedmerchant[$(myRadio).attr("name")] = $(myRadio).attr("id");
  }
  console.log(window.selectedmerchant);
}
&#13;
<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/>
&#13;
&#13;
&#13;

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

我希望这会有所帮助。

答案 1 :(得分:1)

为此,请尝试以下代码:

<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

    }
  }
}