jQuery 无法检索占位符属性值?

发布于 2024-10-03 23:08:10 字数 352 浏览 0 评论 0原文

有没有一种 jQuery 方法可以检索表单输入元素的占位符属性?我已经尝试过显而易见的方法:

alert($('form_input_element').attr('placeholder'));

但由于某种原因,它在 Firefox 和 Chrome 中都返回未定义! 上面的代码可以很好地显示所有其他属性...这是 jQuery 中的错误吗?使用最新版本。有解决方法吗?

更新:好吧,非常令人惊讶的是,另一个 jQuery 插件实际上删除了所有输入元素上的占位符属性...这就是为什么我得到了 undefined。

Is there a jQuery way for me to retrieve the placeholder attribute of a form input element? I've tried the obvious:

alert($('form_input_element').attr('placeholder'));

but it returns undefined in both firefox and chrome for some reason! All other attributes are displayed fine with the above code... is this a bug in jQuery? using the latest version. Is there a workaround?

UPDATE: Ok, very surprisingly another jQuery plugin was actually REMOVING the placeholder attributes on all input elements... That's why I was getting undefineds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不咬妳我踢妳 2024-10-10 23:08:10

我认为问题是您的选择器可能匹配多个输入,并且您的集合中的第一个没有像本示例中那样定义的占位符属性

http://jsfiddle.net/fcalderan/rmN7J/

因此请确保您的选择器返回一个输入,或者您选择的所有输入都定义了占位符属性

I think the problem is that your selector may match more than one input, and the first one of your collection has no placeholder attribute defined like in this example

http://jsfiddle.net/fcalderan/rmN7J/

so be sure that your selector returns one input or all of your inputs selected have placeholder attribute defined

缺⑴份安定 2024-10-10 23:08:10

form_input_element 是类名吗?如果是这样,您需要在其前面添加一个.。 (即$(".form_input_element")

如果它是一个id,那么您需要在它前面加上#。 (即 $("#form_input_element")

请记住,传递给 jQuery 对象的字符串实际上与 CSS 选择器相同,因此必须将其格式化。目前,您的选择器正在查找名为 form_input_element 的元素,但该元素不存在。

Is form_input_element a class name? If so, you need a . in front of it. (ie $(".form_input_element"))

If it's an id, then you need the # in front of it. (ie $("#form_input_element"))

Remember that the string you pass to the jQuery object is virtually identical to a CSS selector, so it must be formatted as such. Currently, your selector is looking for an element named form_input_element which does not exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文