[英]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 == "");
});
$("#comment").on("keyup blur", function() {
$("#box").prop("checked", this.value == "");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.