[英]Form is being submitted in IE11
即使txt_approver
為空,也會提交以下表單。 但是在Firefox / Chrome中效果很好? 我沒有錯誤,可能是什么問題?
<script>
function validateForm() {
var x = document.forms["warningnotice"]["txt_approver"].value;
alert(x);
if (x == null || x == "") {
alert("Please enter a name to send a email to ");
return false;
}
}
</script>
<form method="post" action="travel.cfm" id="commentForm" name="warningnotice" onsubmit=" return validateForm()">
<td ><input type="text" name="txt_approver" class="get_empl" required data-error="#errNm35"></td>
<input type="submit" name="Submit" value="Submit" >
</form>
您正在使用name屬性在JavaScript中查找表單和文本框。 這不是name屬性的作用。 id屬性是為此目的。
代替:
document.forms["warningnotice"]
和
<input type="text" name="txt_approver" ...
這些行應為:
document.forms["commentForm"]
和
<input type="text" name="txt_approver" id="txt_approver"...
並且,代替document.forms
,使用:
var form = document.getElementById("commentForm");
var tb = document.getElementById("txt_approver");
另外,代替:
if (x == null || x == "")
您可以使用:
if (!String.trim(x))
因為一個空的文本框將產生一個空字符串“”和一個空字符串,所以將其轉換為布爾值(if語句所尋找的布爾值)將為false,但將其填充為布爾值將轉換為true。
最后,您可能根本不想讓Submit按鈕具有name屬性,因為提交表單時,submit按鈕的值將與其他表單元素一起提交,這可能不是您想要的。
以下JS Fiddle顯示了在IE 11中對我有用的代碼: https : //jsfiddle.net/x0rbnL5s/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.