2012-04-04 55 views
0

我有一組單選按鈕,我需要檢查是否有人被點擊過,如果沒有,然後拋出一個erorr我如何驗證下面的jquery?如何檢查具有相同組名稱的兩個單選按鈕

<legend>Follow Opening Script?</legend> 
<p> 
    <strong><asp:RadioButton GroupName="OpeningScript" CssClass="inline-radio" ID="rdoOpeningScriptYes" runat="server" Text="Yes" TextAlign="Right" /></strong>&nbsp;&nbsp; 
    <strong><asp:RadioButton GroupName="OpeningScript" CssClass="inline-radio" ID="rdoOpeningScriptNo" runat="server" Text="No" TextAlign="Right" /></strong> 
</p> 
+0

你真的應該發佈實際的HTML,而不是asp標籤。 – ThiefMaster 2012-04-04 07:29:26

+0

http://stackoverflow.com/questions/277589/validation-of-radio-button-group-using-jquery-validation-plugin這會有幫助嗎? – TRR 2012-04-04 07:30:54

回答

0

註冊事件的文件準備:

$('input[name="OpeningScript"]').click(function() { 
    alert($(this).val()); 
}); 

更改索引來獲得特定項目的狀態:

$("input[name*=OpeningScript]")[0].checked 

要獲得正文,請嘗試:

$("input[name*=OpeningScript]:checked + label").text(); 
0

Working FIDDLE Demo

添加button到您的HTML:

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

並寫出其click檢查功能:

$(function() { 
    $('#check').on('click', function() { 
     if (! $('[name="OpeningScript"]:checked').length) { 
      alert('Please choose an option'); 
     } 
    }); 
}); 
相關問題