Javascript 我必须在IE中使用jquery框架按两次提交按钮来提交表单
我在IE浏览器中有一种奇怪的行为 我有简单的表格:Javascript 我必须在IE中使用jquery框架按两次提交按钮来提交表单,javascript,jquery,html,Javascript,Jquery,Html,我在IE浏览器中有一种奇怪的行为 我有简单的表格: <form name="test" id="test" action="some_url_here" method="post"> <input type="text" name="url" id="url" class="required" /> <input type="text" name="page" id="page" class="required" /> ... <inpu
<form name="test" id="test" action="some_url_here" method="post">
<input type="text" name="url" id="url" class="required" />
<input type="text" name="page" id="page" class="required" />
...
<input type="submit" name="submit" value="Submit" />
</form>
假设我正确填写了所有字段
在Chrome和Firefox中,当我按下submit按钮时,一切正常,只需一次
在IE(所有版本)中,我必须在提交表单上按两次以执行/汇总表单
为什么?
我还尝试在isvalidul
条件之后添加:
$(this).submit();
但是没有成功。您有两个选择。在这两种情况下,您都需要停止提交事件并验证表单
- 一种是遍历表单中的所有字段,将类错误添加到无效字段中(如果有),设置变量结果并返回(或者在一切正常的情况下提交)
- 另一种方法是在发现第一个无效字段时停止测试,不使用变量result,然后返回(如果一切正常,则提交)
$(function () {
$("#test").on("submit", function (e) {
// Stop the submit, because you need to validate the form before proceeding.
e.preventDefault();
// Check the comments below to decide for the use of the variable.
var result = true;
$(".required").removeClass("error");
$.each($("input.required"), function (k, v) {
// Test for empty fields or, if it's the URL, check whether is valid.
if ($(this).val() === "" || ($(this).attr("name") == "url" && !IsValidUrl($(this).val())) {
// Add class error to the invalid fields.
$(this).addClass("error");
// At this point, you could just return false stopping the loop,
// or keep going to make sure all the invalid fields will get the
// class error. In this case, you can still use the variable result.
result = false;
// Keep going, so, no return.
// return false;
}
});
// If you decided to carry on through all the fields, and don't return
// false at the first error, test for the result value.
// As this is the case...
if (!result) return false;
else $(this).submit();
// If you didn't return previously...
// $(this).submit();
});
});
如果(isvalidul($([input[name='url']).val())?一个打字错误,我忘记了
!
符号$(“#test”)。在(“提交”上,函数(){
实际提交表单,那么为什么要添加$(此).submit()
。?甚至我也尝试了你的代码,它在IE 7和中为我第一次单击工作8@DipeshParmar:这是可选的,我只是添加了它来检查是否有效。但不起作用:)可能无法解决您的问题,但如果您已经返回true/false,为什么要设置变量结果?对于每个可能的错误,只返回false。否则,返回true。
$(function () {
$("#test").on("submit", function (e) {
// Stop the submit, because you need to validate the form before proceeding.
e.preventDefault();
// Check the comments below to decide for the use of the variable.
var result = true;
$(".required").removeClass("error");
$.each($("input.required"), function (k, v) {
// Test for empty fields or, if it's the URL, check whether is valid.
if ($(this).val() === "" || ($(this).attr("name") == "url" && !IsValidUrl($(this).val())) {
// Add class error to the invalid fields.
$(this).addClass("error");
// At this point, you could just return false stopping the loop,
// or keep going to make sure all the invalid fields will get the
// class error. In this case, you can still use the variable result.
result = false;
// Keep going, so, no return.
// return false;
}
});
// If you decided to carry on through all the fields, and don't return
// false at the first error, test for the result value.
// As this is the case...
if (!result) return false;
else $(this).submit();
// If you didn't return previously...
// $(this).submit();
});
});