[英]check if radiobutton is selected on different/other page with jquery
[英]jQuery, check to see if all radiobutton groups are selected
我有幾個單選按鈕組,並且在檢查它們時需要運行一個腳本。
我使用以下腳本檢查是否選中了其中之一,如果未選中,則為其上色。
我該如何編寫代碼,以便在選中所有單選按鈕組后運行腳本。
檢查單選按鈕組是否被選中的代碼:
$('.aQuestion').each(function(){
if($(this).find('input[type="radio"]:checked').length > 0)
{
alert("checked");
}
else
{
alert("not checked");
}
});
單選按鈕組(大約有90個):
<div class='aQuestion' id='div1'>
<STRONG>1. </STRONG>
<STRONG>Question</STRONG></br>
<INPUT TYPE='radio' NAME='grp1' VALUE='0'>answer 1</br>
<INPUT TYPE='radio' NAME='grp1' VALUE='1'>answer 2</br>
<INPUT TYPE='radio' NAME='grp1' VALUE='2'>answer 3</br>
<INPUT TYPE='radio' NAME='grp1' VALUE='3'>answer 4</br>
<INPUT TYPE='radio' NAME='grp1' VALUE='4'>answer 5
</div>
<div class='aQuestion' id='div2'>
<STRONG>2. </STRONG>
<STRONG>Question</STRONG></br>
<INPUT TYPE='radio' NAME='grp2' VALUE='0'>answer 1</br>
<INPUT TYPE='radio' NAME='grp2' VALUE='1'>answer 2</br>
<INPUT TYPE='radio' NAME='grp2' VALUE='2'>answer 3</br>
<INPUT TYPE='radio' NAME='grp2' VALUE='3'>answer 4</br>
<INPUT TYPE='radio' NAME='grp2' VALUE='4'>answer 5
</div>
提前致謝
假設每個問題只有一個單選按鈕組,則無需迭代問題即可發現所有問題均已選中:
var $questions = $(".aQuestion");
if($questions.find("input:radio:checked").length === $questions.length) {
// All Checked
}
jsFiddle演示了以上內容。
嘗試這個:
$(document).on('ready change','.aQuestion',function(){
if($(this).find('input[type="radio"]:checked').length > 0)
{
alert("checked");
}
else
{
alert("not checked");
}
});
最終代碼:
jQuery('#submit').click(function(event)
{
event.preventDefault();
$('.aQuestion').each(function()
{
if($(this).find('input[type="radio"]:checked').length > 0)
{
$(this).addClass( "madeChoice" ); // Run css that hides the group
}
else
{
$(this).addClass( "didntMakeChoice" ); // Run css that highlight the group.
}
});
var $questions = $(".aQuestion");
if($questions.find("input:radio:checked").length === $questions.length)
{
alert("all checked"); // Send result to DB.
}
else
{
alert("Not Checked"); // Do nothing.
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.