Jquery:有没有办法让val()为空列表返回一个空字符串而不是'undefined'? [英] Jquery: Is there some way to make val() return an empty string instead of 'undefined' for an empty list?

查看:82
本文介绍了Jquery:有没有办法让val()为空列表返回一个空字符串而不是'undefined'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jquery,是否有一些方法可以使val()在对空元素列表调用时返回空字符串而不是'undefined'?

With Jquery, is there some way to make val() return an empty string instead of 'undefined' when called against an empty list of elements?

例如,我有这个代码:

var x = $('#my-textbox-id').not('.watermark').val();

这个想法是我想得到我的文本框的值,但我想要一个空字符串if if它目前正在显示水印(我不想要水印值!)。

The idea is that I want to get the value of my textbox, but I want an empty string if it is currently showing the watermark (i don't want the watermark value!).

选择器有效,但我不想'未定义' - 我想要一个空字符串''。

The selector works, but i don't want 'undefined' - I want an empty string ''.

推荐答案

你可以写(...)。val()||

仅用于文本字段;它还将用 false 或数字 0 >。

Only use this for text fields; it will also replace false or the number 0 with "".

这篇关于Jquery:有没有办法让val()为空列表返回一个空字符串而不是'undefined'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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