2014-07-23 65 views
0

我想要做的是,單擊保存按鈕時,如果用戶選擇了任何缺少單選按鈕但未選擇下拉選項,則需要顯示一條警報消息。單選按鈕和下拉驗證

我試過2種方法。首先是這樣

if ($(".missing input[type:radio]:checked").length > 0 && $(".reason").val() == "") 
    { 
    alert("You have not selected a reason from the drop down list of the Missing section."); 
    return false; 
    } 

第二種方法 - 通過識別textarea中的文本。如果它包含〜CM ~~然後顯示一個提醒

if ("ta:contains(~CM~~).length>0") 
{ 
alert("Missing radio has been selected but nothing in the drop down has been selected"); 
} 

我已經注意到在小提琴中的代碼,並感覺使用任何方法。

JSFIDDLE

回答

1

首先,它不是用<table>來設計你的網站一個很好的方法,因爲抓取工具將無法從您的網站得到任何信息。即使對於這個問題,我添加的假類的<tr>有這些單選按鈕..

更改方法2成這樣:

//for checking the combobox 
$('.selection').each(function(){ 
    if($(this).find(".missing input").attr("checked") == "checked" && $(this).find(".reason").val() == ""){ 
     alert("You have not selected a reason from the drop down list of the Missing section."); 
    } 
}); 
//for checking the textarea 
$('.selection').each(function(){ 
    if($(this).find(".missing input").attr("checked") == "checked" && $(this).find("textarea").val() == ""){ 
     alert("You have not type anything in the textarea."); 
    } 
}); 

看看這個Fiddle

+0

謝謝。不幸的是,我無法向tr添加任何內容。你能否告訴我爲什麼我的第二個代碼(:contains)不起作用。它的目的是檢查textarea是否有〜CM ~~如果它有警報。 – 19eggs

+0

我不完全是爲什麼,但類似的方法是這樣的:'if(ta.indexOf(「〜CM ~~」)> 0)' –