簡體   English   中英

選中已簽入的組單選按鈕 - 驗證

[英]Checking checked in group radio buttons - validate

我有:

<div class="group">
   <input type="radio" name="myradio" value="1">a
   <input type="radio" name="myradio" value="2">b
</div>
<div class="group">
   <input type="radio" name="two" value="3">a
   <input type="radio" name="two" value="4">b
</div>
<div class="group">
   <input type="radio" name="aaa" value="5">a
   <input type="radio" name="aaa" value="6">b
</div>

http://jsfiddle.net/jz84u/2/

如何檢查此示例中是否檢查了最少一個組? 如果沒有那么應該警惕('錯誤')。 我想用jQuery。

$(".group").each(function(){

})

但我怎么檢查這個? 對於我的示例應該是返回錯誤 - 檢查任何輸入,但如果我有:

<div class="group">
   <input type="radio" name="myradio" value="1">a
   <input type="radio" name="myradio" value="2">b
</div>
<div class="group">
   <input type="radio" name="two" value="3">a
   <input type="radio" name="two" value="4" checked="checked">b
</div>
<div class="group">
   <input type="radio" name="aaa" value="5">a
   <input type="radio" name="aaa" value="6">b
</div>

對於這個例子是好的 - 檢查第二組中的輸入。

我怎么驗證這個?

這符合您的需求嗎?

   var n = $(".group input:checked").length;

   if(n>0){do something}

嘗試下面它會工作...

添加一個按鈕

<input type="button" value="check" id="btnCheck">

<div class="group">
   <input type="radio" name="myradio" value="1">a
   <input type="radio" name="myradio" value="2">b
</div>
<div class="group">
   <input type="radio" name="two" value="3">a
   <input type="radio" name="two" value="4">b
</div>
<div class="group">
   <input type="radio" name="aaa" value="5">a
   <input type="radio" name="aaa" value="6">b
</div>

寫下面的jquery

$("#btnCheck").click(function() {

   if($(".group input:radio:checked").val())
       alert("checked");
    else
    alert("Nothing checked");   

});

小提琴: http//jsfiddle.net/jz84u/6/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM