我相信对此一定有一个简单的解释。我试图得到一个警告‘国家必须被选中’时出现的选项‘没有选择’(它是默认的,我相信),并点击验证按钮。我得到了正确显示警报的函数,但问题是警报显示了两次。
为什么警报会显示两次,我怎样才能让它只显示一次呢?谢谢!
function CountrySelectValidation() { // ensures country is selected
var ValidateCountry = document.forms["registration-form"]["Country"].value;
if (ValidateCountry == "not-selected") {
alert("Country must be selected");
return false;
} else {
return true;
}
}
function SubmitForm() {
if (CountrySelectValidation()) {
// check if true, then execute this code
document.getElementById("registration-form").submit();
}
}<form id="registration-form">
What country are you from?
<select name="Country">
<option value="not-selected">Not Selected</option>
<option value="england">England</option>
<option value="wales">Wales</option>
<option value="scotland">Scotland</option>
<option value="northern-ireland">Northern Ireland</option>
</select>
<br><br><input type="submit" value="Submit Method 1">
<button type="reset">Reset Method 1</button>
<button type="submit" id="submit-2">Submit Method 2</button>
</form>
<button onClick="ResetForm()">Reset Method 1</button>
<button onClick="CountrySelectValidation(); SubmitForm()">Validation</button>
发布于 2017-03-25 14:38:05
调用if语句时,将再次执行该函数。在原始函数中调用的不同函数中运行if语句中的代码。
OriginalFunction () {
//Content here
FunctionToHandleIfstatement();
}发布于 2017-03-25 14:36:38
CountrySelectValidation()
...
if (CountrySelectValidation())您可以在CountrySelectValidation内部运行if函数(它实际上会执行),这就是为什么它会运行两次。
https://stackoverflow.com/questions/43017657
复制相似问题