jquery如果文本输入*不*等于空白 [英] jquery if text input does *not* equal blank

查看:62
本文介绍了jquery如果文本输入*不*等于空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个文本框为空(即包含某些内容,可能是任何内容),我正在努力解决如何做某事



<这是我的代码(似乎不起作用)

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆