gpt4 book ai didi

javascript - 如果需要空字段属性

转载 作者:行者123 更新时间:2023-11-30 16:50:16 27 4
gpt4 key购买 nike

我有一个登录表单,我想将字段设为必填,这样就不会发送任何空字段。

问题是密码字段,如果我将其设为必需,javascript 函数将清除它并且不会发送(所需的消息将出现在该字段中)。

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();
}

可能是什么问题?我需要返回吗?

注册和修改密码等其他功能呢?如何简化这么多 if?

最佳答案

您需要使用表单的 onsubmit 事件处理程序而不是 onclick 处理程序,因为 required 标志不会阻止点击事件被触发。

那么你应该在脚本中附加你的事件处理程序而不是内联和......

  1. 阻止表单提交
  2. 然后散列密码值。
  3. 然后重新提交表格

// 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();
}

关于javascript - 如果需要空字段属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com