文本框的 Jquery 验证 OnBlur
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9032943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Validation OnBlur of textbox
提问by user1145150
Javascript:
Javascript:
<script type="text/javascript">
function checkdata() {
$("#TextBoxesGroup :input").each(function () {
if (!$(this).val()) {
alert(this.val()+":Dis");
$("#addButton").attr("disabled", "disabled");
}
else {
alert(this.val() + ":Ena");
$("#addButton").removeAttr("disabled");
}
});
}
$(document).ready(function () {
$("#addButton").attr("disabled", "disabled");
$("#TextBoxesGroup :input").each(function () {
$(this).blur(function () {
checkdata();
});
});
});
</script>
Html
html
<div id='TextBoxesGroup'>
<div style="float: left; width: 625px;">
<div>
<input type="text" name="answer" class="ahsan" />
</div>
</div>
</div>
<div style="clear: both;">
</div>
<div>
<input type="button" value="Add New Row" class="qa-adbtn" id='addButton' />
</div>
Now the thing i want to do is that if user enter something in textbox(ahsan) .then add button enabled and if there is nothing in textbox onblur then add button automatically goes disabled
现在我想要做的是,如果用户在文本框(ahsan)中输入一些内容。然后启用添加按钮,如果文本框 onblur 中没有任何内容,则添加按钮会自动禁用
回答by Fernando Mondo
this works
这有效
function checkdata() {
var ok = true;
$("#TextBoxesGroup :input").each(function () {
if (!$(this).val()) {
ok = false;
return;
}
});
if (ok) $("#addButton").removeAttr("disabled");
else $("#addButton").attr("disabled", "disabled");
}
$(document).ready(function () {
$("#addButton").attr("disabled", "disabled");
$("#TextBoxesGroup :input").blur(function () {
checkdata();
});
});
</script>
</head>
<body>
<div id='TextBoxesGroup'>
<div style="float: left; width: 625px;">
<div>
<input type="text" name="answer" class="ahsan" />
<input type="text" name="answer" class="ahsan" />
</div>
</div>
</div>
<div style="clear: both;">
</div>
<div>
<input type="button" value="Add New Row" class="qa-adbtn" id='addButton' />
</div>
</body>
</html>
回答by Damon Skelhorn
Here you go
干得好
function checkdata() {
$("#TextBoxesGroup :input").each(function() {
if($(this).val().trim().length < 1) {
alert($(this).val() + ":Dis");
$("#addButton").attr("disabled", true);
}
else {
alert($(this).val() + ":Ena");
$("#addButton").attr("disabled", false);
}
});
}
$(function () {
$("#addButton").attr("disabled", true);
$("#TextBoxesGroup input").blur(function () {
checkdata();
});
});
回答by Nayana Setty
Each loop is not required as blur function is bind to each text box.
每个循环都不是必需的,因为模糊功能绑定到每个文本框。
Try this
尝试这个
$("#addButton").attr("disabled", true);
$("#TextBoxesGroup input").blur(function () {
if($(this).val()== "" ) {
$("#addButton").attr("disabled", true);
}
else {
$("#addButton").attr("disabled", false);
}
回答by Dhaval Shukla
Check the following fiddle :
检查以下小提琴:
Use keypress event and if length of txtbox is greater dan enable your button
使用按键事件,如果 txtbox 的长度更大,则启用您的按钮
Edit:
编辑:
In Fiddle i have kept the button as disabled bydefault you can make appropriate changes as per your need..
在 Fiddle 中,默认情况下我将按钮保持为禁用状态,您可以根据需要进行适当的更改。