2014-07-23 66 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)' –