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

时间:2022-08-09 17:03:16

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个文本字段,我希望只有在填写所有文本字段后才能填充复选框。

5 个解决方案

#1


1  

Just do this:

这样做:

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

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

#2


1  

 if(document.getElementById('yourtextBox').value!=' ')
 {
    document.getElementById('checbox').checked=false;
 } 

#3


0  

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

请尝试以下代码。在此代码中,每次在文本字段中键入字符时,将调用该函数并且textfield值的长度不为零,checbox将被取消选中,并在清除文本字段时进行检查

//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"/>

#4


0  

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

EDIT: You can also use jQuery

编辑:你也可以使用jQuery

#5


-1  

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

Try jQuery, This works for me...

试试jQuery,这对我有用......

#1


1  

Just do this:

这样做:

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

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

#2


1  

 if(document.getElementById('yourtextBox').value!=' ')
 {
    document.getElementById('checbox').checked=false;
 } 

#3


0  

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

请尝试以下代码。在此代码中,每次在文本字段中键入字符时,将调用该函数并且textfield值的长度不为零,checbox将被取消选中,并在清除文本字段时进行检查

//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"/>

#4


0  

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

EDIT: You can also use jQuery

编辑:你也可以使用jQuery

#5


-1  

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

Try jQuery, This works for me...

试试jQuery,这对我有用......