简体   繁体   English

如果文本框已填充,如何自动取消选中复选框

[英]How to automatically uncheck checkbox if textbox is filled

I've made the checkbox to be default check. 我已将复选框设为默认复选框。 How can I uncheck the checkbox automatically if certain textfield is filled. 如果填写了某些文本字段,如何自动取消选中该复选框。 I've thinking of using javascript. 我正在考虑使用JavaScript。


I tried using this 我尝试使用这个

<input type="text" name="commentID" id="commentID" onkeyup="userTyped('skipID', this)"/>
<input type="checkbox" name="skipID" value="N" id="skipID" checked="checked"  />

and the javascript 和JavaScript

function userTyped(commen, e){
if(e.value.length > 0){
    document.getElementById(commen).checked=false;
}else{
    document.getElementById(commen).checked=true;
}}​

It works but how if i have 3 textfield, and I want the checkbox to be filled only after all textfield is filled. 它有效,但是如果我有3个文本字段,并且我希望仅在所有文本字段都填满后才填写复选框。

Just do this: 只要这样做:

<input type="checkbox" id="someid" checked="checked"/>

<textarea onchange="if (this.value != '') document.getElementById('someid').checked = false;"></textarea>
 if(document.getElementById('yourtextBox').value!=' ')
 {
    document.getElementById('checbox').checked=false;
 } 

Try the following code. 请尝试以下代码。 In this code, each time when you type a character in the textfield, the function will be called and the length of the textfield value is not zero, the checbox will be unchecked and it will be checked while you clear your textfield 在此代码中,每次在文本字段中键入字符时,都会调用该函数,并且文本字段值的长度不为零,将取消选中复选框,并且在清除文本字段时将选中该复选框

//Function get called when you type each letter
function OnChangeTextField(){
    //getting the length of your textbox
    var myLength = $("#myTextbox").val().length;
    if(myLength > 0){
        $("#myCheckbox").attr("checked", false);
    }else{
           $("#myCheckbox").attr("checked", true);
        }

}


<input type = "text" id = "myTextbox" onkeyup= "OnChangeTextField();"/>
<input type="checkbox" id = "myCheckbox" value = "true" checked = "checked"/>
$("#comment").on("keyup blur", function () {
    $("#box").prop("checked", this.value == "");
});

EDIT: You can also use jQuery 编辑:您也可以使用jQuery

 $("#comment").on("keyup blur", function() {
    $("#box").prop("checked", this.value == "");
});

Try jQuery, This works for me... 试试jQuery,这对我有用...

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

相关问题 如果单击文本框,如何自动取消选中复选框 - How to automatically uncheck a checkbox if the textbox is clicked 填写多个文本框后如何自动取消选中复选框? - How to unchecked checkbox automatically after multiple textbox is filled? 如果通过javascript检查另一个复选框,如何自动取消选中复选框? - How to automatically uncheck a checkbox if another checkbox is checked through javascript? 当使用JavaScript取消选中复选框时,如何启用文本框 - how to enable textbox ,when uncheck a checkbox using javascript 当用户删除文本框中的文本时如何取消选中复选框? - How to uncheck a checkbox when the user deletes the text in the textbox? 如果已经选中了其他复选框,如何使用javascript自动取消选中复选框? - How to automatically uncheck a checkbox with javascript if other is already checked? 如何使用 jquery 取消选中复选框 - how to uncheck a checkbox with jquery 如何取消选中AJAX中的复选框? - How to uncheck the checkbox in AJAX? 取消选中复选框时如何禁用PHP禁用和清除文本框。 选中/取消选中复选框,具体取决于数据库 - How to disable anPHP disable and clear textbox when checkbox is unchecked. Check/uncheck checkbox depending on database 如何设置复选框到达第三个复选框时自动取消选中 - How to set the checkbox uncheck automatically when its reach 3rd checkbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM