[英]How to check if a specific textbox under a specific div is empty
I have this code: 我有这个代码:
<PlaceHolder>
<div class="greenDiv">
<asp:TextBox ID="a" runat="server" />
</div>
<div class="greenDiv">
<asp:TextBox ID="ab" runat="server" />
</div>
</PlaceHolder>
I need to let the user know if he left a Textbox empty, I tried something like this.. (not working) What have I missed? 我需要让用户知道他是否留空文本框,我尝试过这样的事情..(不工作)我错过了什么?
$('.greenDiv > input:text').blur(function () {
if (!$(this).val()) {
alert("fill this field");
}
});
Try using input[type=text]
instead 请尝试使用
input[type=text]
$('.greenDiv>input[type=text]').blur(function () {
if (!$.trim($(this).val())) {
alert("fill this field");
}
});
$('.greenDiv > input[type=text]').on('blur', function (e) {
if (!$(e.currentTarget).val()) alert('fill this field');
});
Instead of input:text
you should use input[type=text]
而不是
input:text
你应该使用input[type=text]
$('.greenDiv>input[type=text]').on("blur",function () { if (!$(this).val()) { alert("fill this field"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="greenDiv"> <input type="text"> </div> <div class="greenDiv"> <input type="text"> </div>
Try this 试试这个
<PlaceHolder>
<div class="greenDiv">
<asp:TextBox ID="a" class="x" runat="server" />
</div>
<div class="greenDiv">
<asp:TextBox ID="ab" class="x" runat="server" />
</div>
</PlaceHolder>
below is Jquery 下面是Jquery
$('.greenDiv .x').blur(function () {
if (!$(this).val()) {
alert("fill this field");
}
});
Try this: 试试这个:
$('.greenDiv > input:text').blur(function () {
if ($.trim($(this).val()) === '') {
alert("fill this field");
}
});
Your selector is fine. 你的选择器很好。 When you use
!$(this).val()
you allow inputs with spaces (or any other invisible character) to fall through. 当你使用
!$(this).val()
你允许带有空格(或任何其他不可见字符)的输入通过。 An input with a single space is technically not empty. 具有单个空格的输入在技术上不是空的。 Try
$.trim(something) === ''
instead. 试试
$.trim(something) === ''
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.