Javascript 如何检查特定div下的特定文本框是否为空

Javascript 如何检查特定div下的特定文本框是否为空,javascript,jquery,Javascript,Jquery,我有以下代码: <PlaceHolder> <div class="greenDiv"> <asp:TextBox ID="a" runat="server" /> </div> <div class="greenDiv"> <asp:TextBox ID="ab" runat="server" /> </div> </PlaceHolder> 您应该使用input[type=text]

我有以下代码:

<PlaceHolder>
<div class="greenDiv">
  <asp:TextBox ID="a"  runat="server" />
</div>
<div  class="greenDiv">
  <asp:TextBox ID="ab" runat="server" />
</div>
</PlaceHolder>

您应该使用
input[type=text]

$('.greenDiv>input[type=text]')。打开(“blur”,函数(){
if(!$(this.val()){
警告(“填写此字段”);
}
});

尝试改用
输入[type=text]

$('.greenDiv>input[type=text]').blur(function () {
    if (!$.trim($(this).val())) {
        alert("fill this field");
    }
});
试试这个

    <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>
试试这个:

$('.greenDiv > input:text').blur(function () {
    if ($.trim($(this).val()) === '') {
        alert("fill this field");
    }
});

你的选择器很好。当您使用
时$(this).val()
允许使用空格(或任何其他不可见字符)输入。从技术上讲,只有一个空格的输入不是空的。试试
$。改为修剪(某物)='

不起作用。。我将其添加到$(document).ready()函数中。。它重要吗?不,它可以添加到
ready()
中。您添加了jQuery吗。请检查您的控制台以查看是否出现任何错误!注意空格和不可见字符。我知道你已经接受了这个答案,但看看我的答案。谢谢你,我也会使用你的答案:-)文本框在两个不同的分区中,每个分区都有相同的class=“greenDiv”-一个文本框
    <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>
 $('.greenDiv .x').blur(function () {
                if (!$(this).val()) {
                    alert("fill this field");
                }
            });
$('.greenDiv > input:text').blur(function () {
    if ($.trim($(this).val()) === '') {
        alert("fill this field");
    }
});