简体   繁体   English

禁用基于文本值的复选框

[英]Disable checkbox based on text value

I need to disable a checkbox when a user enters text into a text area, otherwise it would be active. 当用户在文本区域中输入文本时,我需要禁用一个复选框,否则它将处于活动状态。 I have tried most relevant events but I can't get it to work. 我尝试过最相关的活动,但我无法让它发挥作用。 onkeydown disables for the first press and onchange will work if the user enters something then deletes it. onkeydown禁用第一次按下并且onchange将在用户输入内容然后删除它时起作用。 Nothing seems to disable it after they leave the text area. 在他们离开文本区域后似乎没有禁用它。

<script type="text/javascript">

    function enable_cb(textarea) { 
       if ($(textarea).val() != "" ) { 
        $("input.cmb").removeAttr("disabled"); 
    } 
    else { 
        $("input.cmb").attr("disabled", true); 
    } 
} 

</script>
Comments:<br />
<p><textarea name="issue" id="issue_ta" cols="50" rows="10" class="help" tabindex="2" title="Enter Detailed Description" onchange="enable_cb(this);"></textarea></p>
<p><input name="no_issue" type="checkbox" id="no_issue" class="cmb" />No Issues to Report</p>
<p class="label">Enter Current Vehicle Mileage:</p>
<p><input type="tel" name="record_mileage" class="required" tabindex="3" title="&nbsp;Enter Current Mileage&nbsp;" size="25"/></p>
<p><input type="submit" name="Submit" value="Send"/></p>
</form>

Remove the onclick handler and do : 删除onclick处理程序并执行:

$(function() {
    $("#issue_ta").on('change keyup', function() {
        $("input.cmb").prop("disabled", this.value.length); 
    });
});

FIDDLE 小提琴

This 这个

 $("input.cmb").attr("disabled", true);

should be this 应该是这个

 $("input.cmb").attr("disabled", "disabled");

Why don't you use the blur event for the textArea.. This will make sure the code gets executed once the textarea loses focus.. 为什么不为textArea使用blur事件..这将确保一旦textarea失去焦点就会执行代码。

Try this code.. 试试这个代码..

// Your Markup..
Comments:<br />
<p><textarea name="issue" id="issue_ta" cols="50" rows="10" class="help" tabindex="2" title="Enter Detailed Description" ></textarea></p>
<p><input name="no_issue" type="checkbox" id="no_issue" class="cmb" />No Issues to Report</p>
<p class="label">Enter Current Vehicle Mileage:</p>
<p><input type="tel" name="record_mileage" class="required" tabindex="3" title="&nbsp;Enter Current Mileage&nbsp;" size="25"/></p>
<p><input type="submit" name="Submit" value="Send"/></p>​

// Your jQuery code..
​$(function() {
    $('#issue_ta').on('blur' , function(){
       var val =  $('#issue_ta').val();
        if(val == ''){
           $('#no_issue').attr('disabled', true);                 
        }
        else{
             $('#no_issue').attr('disabled', false);    
        }
    });
});​

You can check this fiddle for a working example http://jsfiddle.net/sushanth009/A46py/ 你可以查看这个小提琴,找到一个有效的例子http://jsfiddle.net/sushanth009/A46py/

Thanks to all. 谢谢大家。 Each response got me in the right direction. 每个回应都让我朝着正确的方向前进。

    <script type="text/javascript">
 function enable_cb(textarea) {
    if ($(textarea).val() !== "") {
        $("input.cmb").prop("disabled", true);
    }
    else {
        $("input.cmb").prop("disabled", false);
    }
}
</script>

     Comments:<br />
<p><textarea name="issue" id="issue_ta" cols="50" rows="10" class="help" tabindex="2" title="Enter Detailed Description" onblur="enable_cb(this);"></textarea></p>
<p><input name="no_issue" type="checkbox" id="no_issue" class="cmb" />No Issues to Report</p>
<p class="label">Enter Current Vehicle Mileage:</p>
<p><input type="tel" name="record_mileage" class="required" tabindex="3" title="&nbsp;Enter Current Mileage&nbsp;" size="25"/></p>
<p><input type="submit" name="Submit" value="Send"/></p>
</form>​

Fiddle link 小提琴链接

"Nothing seems to disable it after they leave the text area." “他们离开文本区后似乎没有禁用它。”

.blur will be helpful in this situation - it will detect when the user leaves the element, and you can then apply your desired effect. .blur在这种情况下会有所帮助 - 它会检测用户何时离开元素,然后您可以应用所需的效果。 .blur documentation .blur文档

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

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