Cro*_*ros 101 jquery css-selectors
如何使用jQuery识别空文本框?如果可能的话,我想使用选择器来做.此外,我必须选择id,因为在我想要使用它的真实代码中我不想选择所有文本输入.
在我的以下两个代码示例中,第一个示例准确地显示用户在文本框"txt2"中键入的值.第二个示例确定存在空文本框,但如果填写它仍然将其视为空.为什么是这样?
这可以仅使用选择器来完成吗?
此代码报告文本框"txt2"中的值:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#cmdSubmit').click(function() {
alert($('[id=txt2]').val());
});
});
</script>
</head>
<body>
<form>
<input type="text" name="txt1" id="txt1" value="123" /><br />
<input type="text" name="txt2" id="txt2" value="" /><br />
<input type="text" name="txt3" id="txt3" value="abc" /><br />
<input type="submit" name="cmdSubmit" id='cmdSubmit' value="Send" /><br />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
此代码始终将文本框"txt2"报告为空:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#cmdSubmit').click(function() {
if($('[id^=txt][value=""]').length > 0) {
if (!confirm("Are you sure you want to submit empty fields?")) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
}
});
});
</script>
</head>
<body>
<form>
<input type="text" name="txt1" id="txt1" value="123" /><br />
<input type="text" name="txt2" id="txt2" value="" /><br />
<input type="text" name="txt3" id="txt3" value="abc" /><br />
<input type="submit" name="cmdSubmit" id='cmdSubmit' value="Send" /><br />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Rus*_*Cam 193
其他方式
$('input:text').filter(function() { return $(this).val() == ""; });
Run Code Online (Sandbox Code Playgroud)
要么
$('input:text').filter(function() { return this.value == ""; });
Run Code Online (Sandbox Code Playgroud)
要么
// WARNING: if input element does not have the "value" attribute or this attribute was removed from DOM then such selector WILL NOT WORK!
// For example input with type="file" and file does not selected.
// It's prefer to use "filter()" method.
// Thanks to @AaronLS
$('input:text[value=""]');
Run Code Online (Sandbox Code Playgroud)
演示代码
jQuery的
$(function() {
$('#button').click(function() {
var emptyTextBoxes = $('input:text').filter(function() { return this.value == ""; });
var string = "The blank textbox ids are - \n";
emptyTextBoxes.each(function() {
string += "\n" + this.id;
});
alert(string);
});
});
Run Code Online (Sandbox Code Playgroud)
Jam*_*man 26
您也可以通过定义自己的选择器来实现:
$.extend($.expr[':'],{
textboxEmpty: function(el){
return $(el).val() === "";
}
});
Run Code Online (Sandbox Code Playgroud)
然后像这样访问它们:
alert($(':text:textboxEmpty').length); //alerts the number of text boxes in your selection
Run Code Online (Sandbox Code Playgroud)
cle*_*tus 18
$(":text[value='']").doStuff();
Run Code Online (Sandbox Code Playgroud)
?
顺便说一下,你的电话:
$('input[id=cmdSubmit]')...
Run Code Online (Sandbox Code Playgroud)
可以大大简化和加速:
$('#cmdSubmit')...
Run Code Online (Sandbox Code Playgroud)
thi*_*der 12
如排名靠前的帖子所述,以下内容适用于Sizzle引擎.
$('input:text[value=""]');
Run Code Online (Sandbox Code Playgroud)
在注释中,注意到删除:text
选择器的一部分会导致选择器失败.我相信正在发生的事情是Sizzle在可能的情况下实际上依赖于浏览器的内置选择器引擎.何时:text
添加到选择器,它将成为非标准的CSS选择器,因此必须由Sizzle本身处理.这意味着Sizzle会检查INPUT的当前值,而不是源HTML中指定的"value"属性.
所以这是一个聪明的方法来检查空文本字段,但我认为它依赖于特定的滋滋声引擎的行为(即使用INPUT,而不是在源代码中定义的属性的当前值).虽然Sizzle可能会返回与此选择器匹配的document.querySelectorAll
元素,但只会返回value=""
HTML 中包含的元素.买者自负.