2017-03-31 74 views
-3

如何檢查一個單選按鈕被選中或者不使用jquery ???如何檢查一個單選按鈕被選中或者不使用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> 
+0

不工作這就是爲什麼我問的人 – Manoj

+0

顯示一些相關的HTML – rahulsm

+1

這可能意味着你的代碼沒有正確調用 –

回答

2

檢查這一點,

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/>

我已經採取了只有三個例子只是作爲演示,但檢查的代碼,它工作正常現在。

我希望這會有所幫助。

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 

    } 
    } 
} 
相關問題