javascript Jquery 验证并发送带有链接的表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9375901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:27:58  来源:igfitidea点击:

Jquery Validate and send form with a link

javascriptjqueryjquery-validate

提问by del_dan

This is my code:

这是我的代码:

<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
     $("#alta").validate();
});
</script>
<form id="alta" method="post" action="<?= $this->Html->url('/'); ?>alta">
    <label>Nombre:</label>
    <input type="text" id="name" class="field required" name="data[name]">
</form>
<a href="#" onClick="alta.submit(); return false;" id="button" class="button">Send</a>

And my problem is I do not use submit input, need to use a link, but submits the form and not valid.

而我的问题是我不使用提交输入,需要使用链接,但提交的表单无效。

thanks

谢谢

回答by Rory McCrossan

Without a submit button you need to check the form is valid manually.

如果没有提交按钮,您需要手动检查表单是否有效。

Firstly, remove the onclickattribute from your HTML - this is not a good method of attaching events.

首先,onclick从 HTML 中删除该属性 - 这不是附加事件的好方法。

<a href="#" id="button" class="button">Send</a>

Then place this in your jQuery code:

然后把它放在你的 jQuery 代码中:

$("#button").click(function() {
    if ($("#alta").valid()) {
        $("#alta").submit();
    }
});

回答by JIA

inside the click handler validate the form.

在点击处理程序中验证表单。

   $("#button").click(function(e){
    e.preventDefault();
    if($("#alta").valid()){
        //submit
        $("#alta").submit();
    } 
    else
       return false;
  });

also (after reading the comments) you dont need to validate the form in document ready

此外(阅读评论后)您不需要验证文档中的表单就绪