下面是我的javascript代码 - 只要点击代理单选按钮,它就会进入第一个验证部分而不会检查其他部分:
function validate(form1)
{
var agent1=document.getElementById('agent');
var confirm=document.getElementById('confirm');
var holding=document.getElementById('holding');
var confirm=form1.confirm.value;
var book_by=form1.bookby.value;
var bkother=form1.bookbyother.value;
var agntphn=form1.phone1.value;
var gstnam=form1.guest_name.value;
var gstphn=form1.guest_no.value;
var adlts=form1.no_adults.value;
var children=form1.no_child.value;
var infants=form1.no_infants.value;
var check_in=form1.checkin.value;
var check_out=form1.checkout.value;
var nfd1=form1.no_of_days.value;
var rc=form1.roomtype.value;
var rpd=form1.rp_day.value;
var rpb=form1.extrarate.value;
var tamt=form1.totalamount.value;
var advamt=form1.advance_amount.value;
if ((agent1.checked == true))
{
if(!(book_by || bkother))
{
alert('Choose the valid agent')
}
else if(agntphn =="")
{
alert('Enter the agent phone number')
}
}
else if (gstnam=="") {
alert("Enter a valid Name ");
document.getElementById('guest_name').focus();
return false;
//errors[errors.length] = "Enter valid Name ";
}
else if (!ck_Mobileno.test(gstphn)) {
alert("Enter a valid Mobile Number with 10 digits");
document.getElementById('guest_no').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if ((adlts=="") &&( children=="" )&&( infants=="")) {
alert("Enter a valid No of persons");
document.getElementById('no_adults').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(check_in)) {
alert("Enter a valid Checkin Date");
document.getElementById('checkin').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(check_out)) {
alert("Enter a valid check_out date");
document.getElementById('checkout').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(nfd1)) {
alert("Click to the box to get the no of days");
document.getElementById('no_of_days').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(rc)) {
alert("Select the room type");
document.getElementById('roomtype').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(rpd)) {
alert("Enter the Rate Per Day");
document.getElementById('rp_day').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(rpb)) {
alert("Enter the Rate Per Bed");
document.getElementById('extrarate').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(tamt)) {
alert("Click on the box to get the total amount");
document.getElementById('totalamount').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
else if (!(advamt)) {
alert("Enter the advance amount");
document.getElementById('advance_amount').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
}
</script>
http://services.odata.org/OData/OData.svc/DemoService.Persons
点击确认单选按钮后,所有禁用元素都会显示出来......
答案 0 :(得分:1)
假设您的表单正在执行
<form onsubmit="return validate(this)"
if(!(book_by && bkother)) {
if (agent1.checked) {
if (book_by == "") {
alert('Choose a valid agent');
document.getElementById('bookby').focus();
return false;
}
else {
if (bkother == "") {
alert('Type an agent name');
document.getElementById('bookbyother').focus();
return false;
}
if (agntphn == "") {
alert('Enter the agent phone number');
document.getElementById('phone1').focus();
return false;
}
}
}
if (gstnam=="") {
alert("Enter a valid Name ");
document.getElementById('guest_name').focus();
return false;
//errors[errors.length] = "Enter valid Name ";
}
if (!ck_Mobileno.test(gstphn)) {
alert("Enter a valid Mobile Number with 10 digits");
document.getElementById('guest_no').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if ((adlts=="") &&( children=="" )&&( infants=="")) {
alert("Enter a valid No of persons");
document.getElementById('no_adults').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(check_in)) {
alert("Enter a valid Checkin Date");
document.getElementById('checkin').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(check_out)) {
alert("Enter a valid check_out date");
document.getElementById('checkout').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(nfd1)) {
alert("Click to the box to get the no of days");
document.getElementById('no_of_days').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(rc)) {
alert("Select the room type");
document.getElementById('roomtype').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(rpd)) {
alert("Enter the Rate Per Day");
document.getElementById('rp_day').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(rpb)) {
alert("Enter the Rate Per Bed");
document.getElementById('extrarate').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(tamt)) {
alert("Click on the box to get the total amount");
document.getElementById('totalamount').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
if (!(advamt)) {
alert("Enter the advance amount");
document.getElementById('advance_amount').focus();
return false;
//errors[errors.length] = "Enter valid mobileno ";
}
return true; // allow submission
}
答案 1 :(得分:0)
您正在使用if (condition) {} else if (condition) {}
。匹配的第一个条件是将要执行的所有条件。
如果您希望执行所有测试,请删除else
。
if ((agent1.checked == true))
{
if(!(book_by || bkother))
{
alert('Choose the valid agent');
return false;
}
else if(agntphn =="")
{
alert('Enter the agent phone number')
return false;
}
}
// == > don't use else if here, just a plain if
if (gstnam=="") {
alert("Enter a valid Name ");
document.getElementById('guest_name').focus();
return false;
}
// ... continuing on for the rest of your if statements
如果基于数据,您只想要执行某些测试,那么您必须将代码分解为块并使用正确的if
测试来确定哪些代码执行哪些条件。但是,你拥有它的方式只会执行匹配的第一个条件和其他条件的 none 。