动态输入值的jQuery选择器 [英] jQuery selector on dynamic input value

查看:160
本文介绍了动态输入值的jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在输入字段中使用:contains(),例如

#inputfield 包含一个5.938的动态值,我想用 $(#inputfield:contains('5.938'))



但是这不行,我在的jsfiddle



我不想用 if(element.val()=='5.938'),因为我需要一个临时选择器,而不是使用 if / else 语句。



我也不是指使用<$因为这个值会改变。

解决方案

由于 [value = 5.938] 你需要使用某种有条件的东西。一个简单的选择器不会工作。 .filter() 就足够了,但是:


$ b $

  $('#inputfield')。filter(function()
{
return $(this ).val()=='5.938';
});

就是说,它看起来像你使用ID选择器,它最多可以选择一个元素,因此 .filter()是过度杀毒 - if ... else 在这种情况下确实是最有意义的。


How can you use the :contain() on an input field, e.g.

#inputfield contains a dynamic value of 5.938 and I want to use $("#inputfield:contains('5.938')").

But that won't work, I've provided a demo at jsFiddle.

I don't want to use if(element.val() == '5.938') because I need a provisional selector instead doing an if/else statement.

I also don't mean by using [value=5.938] because the value will change.

解决方案

Due to @Neal's answer, you need to use some sort of conditional. A simple selector won't work. .filter() will suffice, however:

$('#inputfield').filter(function ()
{
    return $(this).val() == '5.938';
});

That said, it looks like you're using an ID selector, which can select at most one element, so .filter() is overkill — if...else really makes the most sense in that case.

这篇关于动态输入值的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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