简体   繁体   English

为什么即使密码匹配后也没有提交我的表格?

[英]why my form is not being submitted even after the passwords match.?

This is my Piece of script with form id="details" 这是我的脚本,形式为id =“ details”

<script type="text/javascript">
function onLoad() {
    $("#password").keyup(passCheck);
    $("#repeatpass").keyup(passCheck);
}
function passCheck() {
    var password = $("#password").val();
    var repeatpass = $("#repeatpass").val();
    if (password == repeatpass) {
        $("#matchpass").text("Passwords match");
        $("#matchpass").addClass("valid");
        $("#matchpass").removeClass("error");
        $("#details").submit(true);

    } else {
        $("#matchpass").text("Passwords donot match");
        $("#matchpass").addClass("error");
        $("#matchpass").removeClass("valid");
        $("#details").submit(false);

    }
}
$(document).ready(onLoad);
</script>

I want to know why my form(id="details") is not being submitted and if someone can explain how this structure works, it will be more helpful for me. 我想知道为什么不提交我的表单(id =“ details”),并且如果有人可以解释此结构的工作原理,则对我会有所帮助。 thanks 谢谢

Working fiddle . 工作提琴

Use $("#details").submit() instead of $("#details").submit(true); 使用$("#details").submit()代替$("#details").submit(true); and remove $("#details").submit(false); 并删除$("#details").submit(false); .

Hope this helps. 希望这可以帮助。

For starters you do not need to have a .submit call in the else block. 对于初学者,您无需在else块中进行.submit调用。

This code works in three steps: 此代码分为三个步骤:

(1) The .ready command tells jQuery that when the page is ready it should execute the function named onLoad . (1) .ready命令告诉jQuery,当页面准备就绪时,应执行名为onLoad的函数。

(2) The onLoad function tells that when a key from the keyboard is pressed then the passCheck function should be called. (2) onLoad函数告诉您,当按下键盘上的某个键时,应该调用passCheck函数。

(3) The passCheck function checks to see if the two texts are equal and if so submits the form, otherwise it does not submit (ie The .submit should not be called at all). (3) passCheck函数检查两个文本是否相等,如果相等,则提交表单,否则不提交(即,根本不应该调用.submit)。

Probably the two passwords are both empty initially so they are equal and the .submit is attempted but an error on that prevents the function a ms the script stops working. 最初两个密码可能都是空的,所以它们是相等的,并且尝试了.submit ,但是由于出现错误而导致脚本停止工作的错误。 After that the script stops working completely. 之后,脚本将完全停止工作。

Both notes on the request for the HTML code and the note on the argument given to the .submit , mentioned above are correct. 上面提到的有关HTML代码请求的注释和提供给.submit的参数的.submit都是正确的。

You should not pass parameter in your submit function remove your $("#details").submit(true); 您不应在submit函数中传递参数,请删除$("#details").submit(true); and $("#details").submit(false); $("#details").submit(false); its not there in jquery API write this fragment of code it will be of better use: 它不在jquery API中编写此代码片段,将更好地使用:

$( "form" ).submit(function(event) {
  if ( $( "span" ).hasClass('error')) {
   alert('prevented');
   event.preventDefault();
 }
 return;
});

Fiddle Link 小提琴链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM