jquery
2016-07-08 74 views 0 likes 
0

我有此代码并正常工作,但是当我清除文本字段复选框仍然检查我该怎么办?复选框禁用当文本框中的金额超过

<form name="form1"> 
    <div class="controlset-pad"> 
     <input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" disabled='disabled'/> 
    </div> 
    <input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" disabled='disabled'/> 
    </div> 


     $('input[name=other_name]').keyup(function(){ 
      if (this.value.length > 0) { 
       $('input[name="others"]').prop('disabled', false) 
      } else { 
       $('input[name="others"]').prop('disabled', true)  
      } 
     }) 

http://jsfiddle.net/H8VPY/47/

+0

建议增加与KEYUP以及其他活动占鼠标事件等。 – Iceman

回答

1

更改您的脚本如下。它只是在禁用控件之前取消选中。禁用复选框取消选中前:

$('input[name=other_name]').keyup(function(){ 
    if (this.value.length > 0) { 
     $('input[name="others"]').prop('disabled', false) 
    } else { 
     $('input[name="others"]').attr('checked', false); 
     $('input[name="others"]').prop('disabled', true)  
    } 
}) 
0

添加更改事件以及鼠标基础的编辑,例如点击右键>剪切等

注意。

$('input[name=other_name]').bind('keyup change', function() { 
 
    if (this.value.length > 0) { 
 
    $('input[name="others"]').prop('disabled', false) 
 
    } else { 
 
    $('input[name="others"]').attr('checked', false); 
 
    $('input[name="others"]').prop('disabled', true) 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form name="form1"> 
 
    <div class="controlset-pad"> 
 
    <input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" disabled='disabled' /> 
 
    </div> 
 

 
    <div class="field4"> 
 
    <label>Name on credit card if different from above</label> 
 
    <input type="text" name="other_name" class="medium" /> 
 
    </div> 
 
</form>

相关问题