[英]if empty field attribute required
我有一个登录表单,我想填写必填字段,因此不会发送任何空字段。
问题是密码字段,如果我要求输入密码,则javascript函数将其清除,并且不会发送出去(所需的msg将出现在该字段中)。
JS函数创建另一个隐藏字段并将哈希密码放在此处,然后必须将密码字段清空(password.value =“” ;;)。
我在想使用jquery attr()是否为空。
function formhash(form, password) {
if (password.value = "") {
password.attr("required", true); // somehow this is not working...
}
// 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 = "password";
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();
}
可能是什么问题? 我需要退货吗?
注册和更改密码等其他功能呢? 我如何简化这么多的假设?
您需要使用表单的onsubmit事件处理程序而不是onclick处理程序,因为required
标志不会阻止click事件被触发。
然后,您应该在脚本中附加事件处理程序,而不是内联和...
// Get the form element
var form = document.getElementById('login');
// Attach the submit event handler to the form element
form.onsubmit = function (e) {
// Stop initial submission event
e.preventDefault();
// Get the password element
var password = document.getElementById('password');
// Update the password element's value with a hashed value
password.value = hex_sha512(password.value);
// Resubmit the form
this.submit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.