![](/img/trans.png)
[英]checking if user input includes a <script> tag with javascript. Unexpected token {
[英]Disable an input with javascript. Checking the code
我正在用javascript和html做客户端。 我有个问题。 我想检查密码和重复密码是否相同。 在那种情况下,我想禁用输入。 我看到了一些代码,但是我不明白为什么它不起作用。 你能帮助我吗? 这是我的HTML:
<input class="form-control form-stacked last" name="repeatPassword" id="repeat" placeholder="Repeat password" type="password">
<input class="btn btn-beats btn-block btn-stacked" value="Sign up" onkeydown="enable()" id = "btnPlaceOrder" type="submit">
<input class="form-control form-stacked" name="password" id = "password" placeholder="Password" type="password" required="true">
然后这是我的javascript函数:
var password = document.getElementById("password");
var repeat = document.getElementById("repeat");
function enable(){
if (password.value() == repeat.value()){
document.getElementById("btnPlaceOrder").disabled = false;
} else{
document.getElementById("btnPlaceOrder").disabled = true;
}
}
准确地说,我在HTML中导入了javascript
HTML:将侦听器添加到密码输入中,因此,随着用户键入,提交按钮填充将更改其状态。
<input id="password" type="password" onkeyup="enable()">
<input id="repeat" type="password" onkeyup="enable()">
<input value="Sign up" id="btnPlaceOrder" type="submit">
JS
function enable(){
// the comparison returns true or false so no need for if/else
document.getElementById("btnPlaceOrder").disabled = password.value !== repeat.value;
}
小提琴: http : //jsfiddle.net/40vvL1em/
好吧,只需在重复输入的onkeyup()
事件中调用函数即可。
<input class="form-control form-stacked last" name="repeatPassword" id="repeat" placeholder="Repeat password" type="password" onkeyup="enable()">
这是JS函数:
var password = document.getElementById("password").value;
var repeat = document.getElementById("repeat").value;
function enable(){
if (password === repeat){
document.getElementById("btnPlaceOrder").disabled = false;
}
else{
document.getElementById("btnPlaceOrder").disabled = true;
}
}
那应该做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.