[英]Calling javascript function on form submit (with jquery form validation)
我有一个使用jQuery Validation插件设置的表单。
看到这里: http : //jsfiddle.net/9q865xof/
在添加插件之前,我通过表单的验证获得了这个(非jquery)javascript函数。
function busregform(form, password) {
// new input for hashed password
var p = document.createElement("input");
// create element
form.appendChild(p);
p.name = "p";
p.type = "hidden";
p.value = hex_sha512(password.value);
// clear plaintext password
password.value = "";
// submit the form
form.submit();
return true;
}
在自己的文件中也带有hex_sha512()函数。
这里的想法是通过busregform()从表单中发布密码以创建哈希密码,然后将其发布以供我的PHP脚本处理。
我尝试将其添加到我的验证jquery代码中:
submitHandler: function(){
var pw = $("#pass").val();
var form = $("#business-reg-form");
busregform(form,pw);
}
运气不好...不确定现在该怎么办。 我在想我应该使用Ajax吗?
提交经过插件验证的表单后,如何调用busregform()?
busregform
方法期望使用dom元素引用form
因此您需要
$('#business-reg-form').validate({ rules: { address: { required: true, minlength: 6 }, email: { required: true, email: true, minlength: 6 }, company: { required: true }, pass: { required: true, minlength: 6 }, confirmpw: { required: true, minlength: 6, equalTo: "#pass" } }, submitHandler: function() { var pw = $("#pass"); var form = $("#business-reg-form"); //pass the dom element reference busregform(form[0], pw[0]); return false; } }); function busregform(form, password) { // Create a new element input, this will be our hashed password field. var p = document.createElement("input"); // Add the new element to our form. form.appendChild(p); p.name = "p"; p.type = "hidden"; p.value = hex_sha512(password.value); // Make sure the plaintext password doesn't get sent. password.value = ""; // Finally submit the form. form.submit(); return true; } if (!window.hex_sha512) { //stub method window.hex_sha512 = function(value) { return 'hex_sha512-' + value; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> <form method="post" name="business_regform" id="business-reg-form" action=""> <p> <input type="text" name="company" id="company" placeholder="company name" required /> </p> <p> <input type="text" name="address" id="address" placeholder="address" required /> </p> <p> <input type="text" name="email" id="email" placeholder="email" required /> </p> <p> <input type="text" name="pass" id="pass" placeholder="password" required /> </p> <p> <input type="text" name="confirmpw" id="confirmpw" placeholder="confirm password" required /> </p> <p> <input type="submit" class="btn" id="business-reg-submit" value="submit" /> </p> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.