处理'input'按钮的信息

JS dealing with 'input' button info

本文关键字:信息 input 处理 按钮      更新时间:2023-09-26

我对JS和html非常陌生,所以如果你觉得这个问题太原始,我很抱歉。

我正在尝试做一个简单的登录-注销页面。我成功地在两个显示器之间切换(一旦一个登录或注销),但我仍然有一个问题:当我按下"登出"键时,如何"删除"上次登入时的用户名和密码详情?

换句话说,我如何设置"密码"answers"文本"输入类型是明确的(没有任何信息在他们里面),使用Java脚本,最好与JQuery?

$(document).ready(function(){
    $('#username').val("")
    $('#password').val("")
})

这将在每次加载页面时清除两个输入。

但是正如Ibu所说,你应该用Php服务器端来处理登录。

如果你想清理所有的输入文本,只需使用一个简单的脚本:

$("input[type=text]").val('');

传递所有text类型的输入,其值为空。

你可以将此绑定到你的取消按钮,甚至在发送表单后与确认按钮绑定。

与取消按钮绑定示例(您将需要一个ID="cancel"的按钮来完成此工作):

$("#cancel").click(function() {
    $("input[type=text]").val('');
});

其他答案都很好…使用.val('')就可以了。

我将稍微超出你的要求,因为它可能对你和其他读者有用。这是一个通用的表单重置函数…

function resetForm(formId) {
    $(':input', $('#' + formId)).each(function() {
        var type = this.type;
        var tag = this.tagName.toLowerCase(); // normalize case
        if (type == 'text' || type == 'password' || tag == 'textarea') {
            // it's ok to reset the value attr of text inputs, password inputs, and textareas
            this.value = "";
        } else if (type == 'checkbox' || type == 'radio') {
            // checkboxes and radios need to have their checked state cleared but should *not* have their 'value' changed
            this.checked = false;
        } else if (tag == 'select') {
            // select elements need to have their 'selectedIndex' property set to -1 (this works for both single and multiple select elements)
            this.selectedIndex = -1;
        }
    });
};