Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表格正在IE11中提交_Javascript_Internet Explorer_Internet Explorer 11 - Fatal编程技术网

Javascript 表格正在IE11中提交

Javascript 表格正在IE11中提交,javascript,internet-explorer,internet-explorer-11,Javascript,Internet Explorer,Internet Explorer 11,即使其txt\u审批人为空,仍将提交以下表单。 但它在firefox/chrome中运行良好? 我没有错,可能是什么问题 <script> function validateForm() { var x = document.forms["warningnotice"]["txt_approver"].value; alert(x); if (x == null || x == "") { alert("Please enter a name

即使其
txt\u审批人
为空,仍将提交以下表单。 但它在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>

函数validateForm(){
var x=document.forms[“warningnotice”][“txt_approver”]。值;
警报(x);
如果(x==null | | x==“”){
警报(“请输入要发送电子邮件的姓名”);
返回false;
}
}

您正在使用name属性在JavaScript中定位表单和文本框。这不是name属性的用途。id属性用于此

而不是:

 document.forms["warningnotice"]
   if (x == null || x == "")

此外,不是:

 document.forms["warningnotice"]
   if (x == null || x == "")
您只需使用:

   if (!String.trim(x))
因为一个空文本框将生成一个空字符串“”,而一个空字符串转换为布尔值(if语句查找布尔值)将为false,而填充的文本框将转换为true

最后,您可能根本不希望submit按钮具有name属性,因为在提交表单时,submit按钮的值将与其他表单元素一起提交,这可能不是您想要的


下面的JS Fiddle显示了在IE 11中为我工作的代码:

两个警报都触发了吗?您尝试过没有警报()吗?是的,警报正在触发,我尝试过没有警报:值不会为null,因此检查无效。开发人员控制台中是否有任何错误?
document.forms
有什么问题?您确定要通过该集合中的
id
获取吗?规范显示了通过
name
.document.forms访问它们当然可以工作,但这是一种更古老的技术。name属性用于提交表单元素及其值。如果没有名称,提交表单时元素将不会提交其数据。ID是通过静态创建的标识符引用任何DOM元素的正确方法。太糟糕了,人们没有尝试解决方案就否决了投票!此外,问题在于IE11,我们都知道IE是如何符合标准的!我现在不能测试IE11。因此,在您的演示中,无法通过
document.forms
?那会让我吃惊的。IE在遵守标准方面做得很好。另外,让我惊讶的是,
String.trim()
可以在IE中工作。也许他们添加了它,但这是一个非标准的Firefox功能,我认为它已经被弃用了。我可以告诉你的是,IE(所有版本)众所周知不遵循标准(这就是为什么MS终止了它并创建了Edge)。简单地更改代码以通过id访问textbox元素,使代码在IE 11中对我起作用,并且由于id的唯一目的是用于元素标识(而不是名称的目的),这非常有意义。trim()只是一个建议,与问题无关。
   if (x == null || x == "")
   if (!String.trim(x))