简体   繁体   English

禁用使用javascript的输入。 检查代码

[英]Disable an input with javascript. Checking the code

I'm doing a client side in javascript and html. 我正在用javascript和html做客户端。 I have a problem. 我有个问题。 I want to check if password and repeat password are the same. 我想检查密码和重复密码是否相同。 In that case I want to disable an input. 在那种情况下,我想禁用输入。 I saw some codes around but I don't understand why it doesn't work. 我看到了一些代码,但是我不明白为什么它不起作用。 Can you help me? 你能帮助我吗? Here is my 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">

And then here is my javascript function: 然后这是我的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;
      }
}

Just to be precise, I did the import of the javascript in my HTML 准确地说,我在HTML中导入了javascript

HTML: Add listeners to the password inputs, so the submit button fill change its state as the user types. HTML:将侦听器添加到密码输入中,因此,随着用户键入,提交按钮填充将更改其状态。

<input id="password" type="password" onkeyup="enable()">
<input id="repeat" type="password" onkeyup="enable()">
<input value="Sign up"  id="btnPlaceOrder" type="submit">

JS JS

function enable(){
    // the comparison returns true or false so no need for if/else
    document.getElementById("btnPlaceOrder").disabled = password.value !== repeat.value;
}

Fiddle: http://jsfiddle.net/40vvL1em/ 小提琴: http : //jsfiddle.net/40vvL1em/

Well simply call your function in the onkeyup() event of your repeat input. 好吧,只需在重复输入的onkeyup()事件中调用函数即可。

<input class="form-control form-stacked last" name="repeatPassword" id="repeat" placeholder="Repeat password" type="password" onkeyup="enable()">

And here's the JS function: 这是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;
}
}

That should do it. 那应该做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM