javascript - I have to press submit button twice in IE using jquery framework to submit form -
i have strange behaviour in ie browser.
i have simple form:
<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>
and in js:
var result = true; $("#test").on("submit", function(){ $(".required").removeclass("error"); $.each($("input.required"), function(k, v) { if($(this).val() === '') { $(this).addclass("error"); result = false; return false; } }); if(result) { if(!isvalidurl($("input[name='url']").val()){ $("input[name='url']").addclass("error"); return false; } } else { return false; } });
let's assume filled fields correctly.
in chrome & firefox, when press on submit button works fine, once time.
in ie (all versions) have press 2 times on submit form execute/sumbit form.
why ?
i tried put after isvalidurl
condition:
$(this).submit();
but no success.
you have 2 options here. on both need stop submit event , validate form.
one go through fields in form, add class error invalid ones (if there's any), set variable result , return (or submit if alright).
the other stop test @ first invalid field found, not using variable result, , return (or submit if alright).
js
$(function () { $("#test").on("submit", function (e) { // stop submit, because need validate form before proceeding. e.preventdefault(); // check comments below decide use of variable. var result = true; $(".required").removeclass("error"); $.each($("input.required"), function (k, v) { // test empty fields or, if it's url, check whether valid. if ($(this).val() === "" || ($(this).attr("name") == "url" && !isvalidurl($(this).val())) { // add class error invalid fields. $(this).addclass("error"); // @ point, return false stopping loop, // or keep going make sure invalid fields // class error. in case, can still use variable result. result = false; // keep going, so, no return. // return false; } }); // if decided carry on through fields, , don't return // false @ first error, test result value. // case... if (!result) return false; else $(this).submit(); // if didn't return previously... // $(this).submit(); }); });
Comments
Post a Comment