只有当用户开始在输入字段中键入时,如何从输入值中删除某些警告文本



在表单验证后,我有一个值在输入字段中被php回显,如下所示:

x - Confirm valid surname

如果我想在用户开始在输入字段内键入时(最好是在更改为有效姓氏时(删除匹配的文本" - Confirm valid surname"(这将是一条警告消息(,那么适合使用什么代码(仅限Javascript(?

您的"输入"是这样的吗?

<input value="<?php echo $errorMessage; ?>"/>

我建议使用"占位符"属性

<input placeholder="<?php echo $errorMessage; ?>"/>

或者如果你想继续使用JS,

var inputBox = document.getElementById(INPUT_BOX_ID);
inputBox.oninput = function(){
inputBox.value = inputBox.value.replace(" - Confrim valid surname",""); // Simply remove the Error Text you've echoed
inputBox.oninput = null; //  Delete the function so user can type something
}
// Its still not detecting the valid surname, but its removes the " - Confrim valid surname"

最新更新