如果这两个值都有效,如何避免表单提交
您好,下面是我用于表单验证在两个输入字段中输入的 ID 的代码。所有代码都工作正常。唯一的问题是,如果两个字段都无效,因为我正在禁用和启用,如何停止表单提交如果记录返回为 true,则提交按钮。但如果任何一个字段有效,则提交按钮将启用并允许用户提交表单。
<form>
<input type="text" id="1" value=""/>
<input type="text" id="2" value=""/>
<input type="submit" value="submit" id="submitbutton" >
</form>
<script type="text/javascript">
$$(document).ready(function() {
$$('#1').blur(function() {
var id = $$('#1').val();
$$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res1,code) {
if(res1.ROWCOUNT > 0){
for(var i=0; i<res1.ROWCOUNT; i++) {
s = "<span class='dPostTxt'>" + res1.DATA.ID[i] + " " + res1.DATA.FIRST_NAME[i] + " " + res1.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
};
$$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button
}
else {
$$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button
alert(" ID Is Not Valid.Please Enter Valid ID");
var s = "Entered ID Is Not Valid.";
}
$$("#results").html(s);
},"json");
}); });
</script>
<script type="text/javascript">
$$(document).ready(function() {
$$('#2').blur(function() {
var id = $$('#2').val();
$$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res,code) {
if(res.ROWCOUNT > 0){
for(var i=0; i<res.ROWCOUNT; i++) {
s = "<span class='dPostTxt'>" + res.DATA.ID[i] + " " + res.DATA.FIRST_NAME[i] + " " + res.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
};
$$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button
}
else {
$$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button
alert(" ID Is Not Valid.Please Enter Valid ID");
var s = "Entered ID Is Not Valid.";
}
$$("#results1").html(s);
},"json");
}); });
</script>
Hi Below is the code that i am using for my form to validate id's entered in two input fields.All code is working fine.Only thing is that how to stop form from submitting if both the fields are not valid as i am disabling and enabling submit button if record return is true.but if any one field is valid then submit button is enabling and allowing user to submit form.
<form>
<input type="text" id="1" value=""/>
<input type="text" id="2" value=""/>
<input type="submit" value="submit" id="submitbutton" >
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#1').blur(function() {
var id = $('#1').val();
$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res1,code) {
if(res1.ROWCOUNT > 0){
for(var i=0; i<res1.ROWCOUNT; i++) {
s = "<span class='dPostTxt'>" + res1.DATA.ID[i] + " " + res1.DATA.FIRST_NAME[i] + " " + res1.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
};
$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button
}
else {
$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button
alert(" ID Is Not Valid.Please Enter Valid ID");
var s = "Entered ID Is Not Valid.";
}
$("#results").html(s);
},"json");
}); });
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#2').blur(function() {
var id = $('#2').val();
$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res,code) {
if(res.ROWCOUNT > 0){
for(var i=0; i<res.ROWCOUNT; i++) {
s = "<span class='dPostTxt'>" + res.DATA.ID[i] + " " + res.DATA.FIRST_NAME[i] + " " + res.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
};
$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button
}
else {
$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button
alert(" ID Is Not Valid.Please Enter Valid ID");
var s = "Entered ID Is Not Valid.";
}
$("#results1").html(s);
},"json");
}); });
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的检查指定为在
$("form").submit()
上触发,而不是在字段的 onBlur 上触发。这样您就可以同时检查两种状态,如果您的表单值错误等,可能会向页面发出警报。assign your checks to fire on
$("form").submit()
instead of onBlur of the fields. that way you can check both states at the same time, maybe fire a alert to the page if your form values are wrong &etc.