jquery如果文本输入*不*等于空白 [英] jquery if text input does *not* equal blank
问题描述
如果某个文本框不为空(即包含某些内容,可能是任何内容),我正在努力解决如何做某事
<这是我的代码(似乎不起作用)
if(!($('#edit-sPostalCode') .attr('val',''))){
stuff here
}
我错过了什么?
if($('#edit- sPostalCode')。val()!=''){
stuff here
}
$('#edit-sPostalCode')。attr('val','')
实际上会创建一个值为<$的输入框的属性c $ c>''然后将返回一个jQuery对象。
说!($('#edit-sPostalCode')。attr('val',''))
然后将否定该jQuery对象。作为对象的一个实例是JS中的 truthy 此表达式的结果将始终为 false
。
I am trying to work out how to do something if a certain text box is not empty (i.e. contain something, could be anything)
This is my code (that doesnt seem to work)
if ( !($('#edit-sPostalCode').attr('val','')) ) {
stuff here
}
What have I missed?
if ( $('#edit-sPostalCode').val() != '' ) {
stuff here
}
$('#edit-sPostalCode').attr('val','')
will actually create an attribute of the input box with a value of ''
and will then return a jQuery object.
Saying !($('#edit-sPostalCode').attr('val',''))
will then negate that jQuery object. As an instance of an object is truthy in JS the result of this expression will always be false
.
这篇关于jquery如果文本输入*不*等于空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!