当从属元素的值更改时,禁用输入元素

时间:2019-07-16 14:36:57

标签: javascript jquery

<input type="password" placeholder="oldPass" id="passwordcyu" class="input-xlarge" name="passwordcyu">

<input type="password" placeholder="newPass" disabled=true class="input-xlarge" name="passwordmoi" id="passwordmoi">

<input type="password" placeholder="confirmPass" disabled=true class="input-xlarge" name="cfpw" id="cfpw" style="display:inline">

大家好,我是javascript / jquery的新手,我有3个上面的输入文本。当#oldPass的值更改时,我不知道如何禁用输入#newPass和#confirmPass。请帮助我或给我一些建议。

P / s:这是我第一次对stackoverflow提出疑问。抱歉,如果我让别人对我的问题感到不舒服。

3 个答案:

答案 0 :(得分:1)

我将假定既然您的元素已在HTML中已禁用,则您可能希望启用。 (这也适合隐含的用例。)

最简单的基于jQuery的答案是将元素绑定到change()处理程序,并根据需要禁用输入:

$("#passwordcyu").change(function() {
  $("#passwordmoi, #cfpw").removeAttr("disabled");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="password" placeholder="oldPass" id="passwordcyu" class="input-xlarge" name="passwordcyu">

<input type="password" placeholder="newPass" disabled=true class="input-xlarge" name="passwordmoi" id="passwordmoi">

<input type="password" placeholder="confirmPass" disabled=true class="input-xlarge" name="cfpw" id="cfpw" style="display:inline">

答案 1 :(得分:1)

纯JavaScript实时示例:

function changeInputs() {
  document.getElementById("passwordcyu").disabled = true;
  document.getElementById("passwordmoi").disabled = false;
  document.getElementById("cfpw").disabled = false;
}
<input type="password" placeholder="oldPass" id="passwordcyu" onchange="changeInputs()">

<input type="password" placeholder="newPass" disabled="true" id="passwordmoi">

<input type="password" placeholder="confirmPass" disabled="true" id="cfpw">

答案 2 :(得分:1)

每当单击旧密码时,您都可以使用propdisabled其他输入。即:

//when oldpass is click 
$("#passwordcyu").on("click",function(){
$("#cfpw , #passwordmoi").prop("disabled", true);
});
//when new password is click enbaled it 
$('#passwordmoi ').closest("div").click(function () {
   $(this).find("#cfpw,#passwordmoi").attr("disabled", false);
   
   
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="password" placeholder="oldPass" id="passwordcyu" class="input-xlarge" name="passwordcyu"><br/><br/>
<div>
<input type="password" placeholder="newPass"  class="input-xlarge" name="passwordmoi" id="passwordmoi"> <br/><br/>

<input type="password" placeholder="confirmPass" class="input-xlarge" name="cfpw" id="cfpw" style="display:inline">
</div>