Jquery 如何根据另一个非空输入字段的值设置两个必填字段
我不熟悉jQuery。 我必须根据另一个非空字段创建一个必填字段。 目前我有三个领域-国家代码,州代码和居住 AAA)如果居住号码不为空且州代码和国家代码为空,则应显示错误。这目前适用于一个字段,但不适用于另一个字段。我怎样才能在其他领域工作呢 代码-Jquery 如何根据另一个非空输入字段的值设置两个必填字段,jquery,html,Jquery,Html,我不熟悉jQuery。 我必须根据另一个非空字段创建一个必填字段。 目前我有三个领域-国家代码,州代码和居住 AAA)如果居住号码不为空且州代码和国家代码为空,则应显示错误。这目前适用于一个字段,但不适用于另一个字段。我怎样才能在其他领域工作呢 代码- <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script&g
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script>
$( document ).ready(function() {
console.log("Hello1");
$("#step1").validate({
rules: {
countrycode1: {
required: {
depends: function(element){
return $("#residence").val()!="";
console.log("Hello5");
}
}
}
}
});
});</script>
</head>
<body>
<form id="step1" action="#" method="post">
<label>Country Code</label>
<input type="text" id="countrycode1" name="countrycode1" value="91"/>
</br>
<label>Std Code</label>
<input type="text" name="stdcode" id="stdcode" value="80"/>
</br>
<label>Residence</label>
<input type="text" name="residence" id="residence" />
</br>
<input id="submit" type="submit" value="next step" name="submit"/>
</form>
</body>
</html>
$(文档).ready(函数(){
控制台日志(“Hello1”);
$(“#步骤1”).validate({
规则:{
国家代码1:{
所需:{
依赖:函数(元素){
返回$(“#住所”).val()!=”;
控制台日志(“Hello5”);
}
}
}
}
});
});
国家代码
标准代码
住宅
您只为一个字段设置了规则。您需要在stdcode的规则上有另一个属性。使用此属性
但是如果我设置一个,它就不起作用了。你能帮我摆弄小提琴吗?你把要求颠倒了。当另一个字段为空时,我想显示消息-“此字段是必需的”
$('#submit').click(function(){
if($('#residence').val()!='')
{
if($('#countrycode1').val()!="91" && $('#countrycode1').val()!='')
{
if($('#stdcode').val()=="80" ||$('#stdcode').val()=='')
{
$('#stdcode').next().text( "this Field is Required" );
}
}else if($('#stdcode').val()!="80" && $('#stdcode').val()!='')
{
if($('#countrycode1').val()=="91" ||$('#countrycode1').val()=='')
{
$('#countrycode1').next().text( "this Field is Required" );
}
}
}
});