我正在尝试显示隐藏texbox并在选中单选按钮时验证文本框"是"。选中单选按钮"否"不应显示错误消息。显示/隐藏对我来说很好,但验证texbox对我不起作用。当单选按钮被选中时显示错误消息no并且文本框被隐藏。我是jquery的新手请帮助我。这是代码。
$(function() {
$("input[name='chkPassPort']").click(function () {
if ($("#chkYes").is(":checked")) {
$("#dvPassport").show();
} else {
$("#dvPassport").hide();
}
});
$("#XISubmit").click(function(){
var project1=document.forms["XIForm"]["project1"].value;
var funding1=document.forms["XIForm"]["funding1"].value;
if( project1==null || project1=="") {alert("Please enter Project title"); return false;}
if(funding1==null || funding1=="") {alert("Please enter Funding Agency"); return false;}
document.getElementById("XIForm").submit();
});
});
<form name="XIForm" id="XIForm" method="POST" action="" style="margin-bottom:-10px;">
<input type="radio" id="chkYes" name="chkPassPort" value="Yes" />
Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo" name="chkPassPort" value="No"/>
No
</label>
<div id="dvPassport" style="display: none">
<table cellpadding="10px">
<tr>
<td style="width:250px">Project Title</td>
<td style="width:250px">Funding agency</td>
</tr>
<br/>
<tr>
<td><input type="text" name="project1" id="project1" style="width: 220px;border:solid 1px #0096ce;margin-right:40px;padding: 5px 3px; " maxlength="80"/></td>
<td><input type="text" name="funding1" id="funding1" style="width: 220px;border:solid 1px #0096ce;padding: 5px 3px;margin-right:28px; " maxlength="80"/></td>
</tr>
</table></div>
答案 0 :(得分:0)
if ($("#chkYes").is(":checked"))
{
if( project1==null || project1=="") {alert("Please enter Project title"); return false;}
if(funding1==null || funding1=="") {alert("Please enter Funding Agency"); return false;}
document.getElementById("XIForm").submit();
}
提交前检查单选按钮。 希望它有所帮助。
答案 1 :(得分:0)
通过以下方法,您可以检查值
var project1= $("#<%= project1.ClientID %>").val();
if(project1== "")
{
alert("Please enter a name");
}
else{
alert("Welcome: "+name);
}
var funding1= $("#<%= funding1.ClientID %>").val();
if(funding1== "")
{
alert("Please enter a name");
}
else{
alert("Welcome: "+name);
}
如果你想单独验证隐藏和显示文本框,那么你可以在一个函数中获得这个验证并调用特定的函数
示例:
function CheckProject1Text1()
{
var project1= $("#<%= project1.ClientID %>").val();
if(project1== "")
{
alert("Please enter a name");
}
else{
alert("Welcome: "+name);
}
}
function CheckFunding1Text1()
{
var funding1= $("#<%= funding1.ClientID %>").val();
if(funding1== "")
{
alert("Please enter a name");
}
else{
alert("Welcome: "+name);
}
}