0
我正在驗證從一個onclick兩個控件。這兩個控件都是必需的Javascript驗證兩個控件onclick但不保留價值,如果只有一個控件驗證
Onclick調用validateLocation函數,該函數如果有效,則調用validateForm函數。
我的問題:
如果用戶選擇的狀態,並點擊提交,JavaScript的正常工作,完善。
但
如果用戶選擇了一個研討會的位置,但沒有狀態時,JavaScript會火,但已經對位置所做的選擇將被清除。我需要該選擇才能保持選定狀態,因此用戶不必重複點擊即可。
我的問題:
如何我保留值如果只有兩個條件之一得到滿足?下面
代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type ="text/javascript" language="javascript">
<!--
function validateLocation() {
var radios = document.getElementsByName('RadioGroup1')
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
validateForm();
return true;
}
};
// not checked, show error
document.getElementById('ValidationError').innerHTML = 'Required';
alert("Please select a seminar location");
return false;
}
function validateForm() {
if(document.form1.State.selectedIndex==0)
{
alert("Please select a State");
document.form1.State.focus();
return false;
}
return true;
};
-->
</script>
</head>
<body>
<div>
<form name="form1" onSubmit="return validateLocation();">
<div>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="text-align:right">state: </td>
<td>
<select name="State" id="State">
<option value="">Select</option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
</select>
</td>
</tr>
</table>
</div>
<div>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"><strong>Choose a seminar location</strong></td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="RadioGroup1" value="Fayetteville Senior Center" id="RadioGroup1_0">
</td>
<td><strong>Location One</strong></td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="RadioGroup1" value="Springdale Holiday Inn" id="RadioGroup1_1">
</td>
<td><strong>Location Two</strong></td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="RadioGroup1" value="The Riordan Hall" id="RadioGroup1_2">
</td>
<td><strong>Location Three</strong></td>
</tr>
</table>
<div id="ValidationError" name="ValidationError"></div>
</div>
<div>
<input type="submit" name="button" id="button" value="Submit">
</div>
</form>
</div>
</body>
</html>
正是我一直在尋找。非常感謝你的幫助! – mduschak 2012-03-19 20:34:53