I have a checkbox which i would like to check if the user fills in the text box that follows it. This must be a min of 5 chara开发者_开发百科cters for it to check the checkbox.
<input type="checkbox" id="olt3" name="olt3" <?php if (!empty($mychecklist->pro)) echo 'checked' ?>>
<input type="text" id="pro" name="pro" value="<?php echo $mychecklist->pro?>">Please supply your ID
thanks in advance for any help.
Live Demo
Basically you just need to check the length of the input on keyup, if it meets your criteria use prop to set the checked property to true.
$('#pro').keyup(function(){
if(this.value.length > 4){
$('#olt3').prop('checked', true);
}else{
$('#olt3').prop('checked', false);
}
});
I thinks this will do the trick:
$("#pro").bind("keyup", function(){
var checked = ( $(this).val().length >= 5 );
$("#olt3").attr("checked", checked);
});
http://jsfiddle.net/aalouv/S7cNM/
$(document).ready(function() {
$("pro").keypress(function()
{
$('#olt3').checked = $(this).val().length >=5 ? true : false;
});
});
I hope this helps
First get the length of the text in the input field $("#pro").val().length, if the length is greater or equal to 5, than check the checkbox $("#olt3").prop("checked", true).
$("input").keyup(function () {
var length = $("#pro").val().length;
if(length >= 5){
$("#olt3").prop("checked", true);
}
}).keyup();
加载中,请稍侯......
精彩评论