根据用户输入使用 Jquery 显示/隐藏字段
<li class="numeric optional" id="contest_max_retweet_input"><label for="contest_max_retweet">Winning retweet number</label><input id="contest_max_retweet" name="contest[max_retweet]" size="50" type="text" /></li>
<li class="numeric optional" id="contest_numofwinners_input"><label for="contest_numofwinners">Number of winners (1-100)</label><input id="contest_numofwinners" name="contest[numofwinners]" size="50" type="text" /></li>
如果用户在“contest_max_retweet_input”字段中指定了一个值,如何使用 JQuery 隐藏“contest_numofwinners_input”字段?
<li class="numeric optional" id="contest_max_retweet_input"><label for="contest_max_retweet">Winning retweet number</label><input id="contest_max_retweet" name="contest[max_retweet]" size="50" type="text" /></li>
<li class="numeric optional" id="contest_numofwinners_input"><label for="contest_numofwinners">Number of winners (1-100)</label><input id="contest_numofwinners" name="contest[numofwinners]" size="50" type="text" /></li>
How can I use JQuery to Hide the field: contest_numofwinners_input if there is a value specified by the user in the field: contest_max_retweet_input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试类似的操作:
JS Fiddle demo
这仅隐藏
input
元素其本身,不是包含li
或相应的label
元素。You could try something like:
JS Fiddle demo
This hides only the
input
element itself, not the containingli
, or correspondinglabel
, element.像这样的东西
Something like
更改的问题是您必须保留输入(模糊)才能触发该函数。这就是为什么我更喜欢keyup。
The problem with change is that you have to leave the input (blur) in order for the function to fire. That's why I prefer keyup.
我会根据变化值的否定进行切换。我还将继续并在加载时触发事件处理程序以提供初始显示/隐藏状态(使用名称空间事件以避免导致其他绑定功能执行)。
(编辑:我喜欢布兰登·布恩(Brandon Boone)关于即时操作的按键操作所做的事情,并向我的代码中的事件名称添加了注释。但是,它会增加一点开销,因为该函数针对键盘上的每个笔划运行,如下所示与字段模糊时相反。)
演示:
http://jsfiddle.net/JAAulde/fUKfb/3/
代码:
I would toggle based on negation of value at change. I'd also go ahead and trigger the event handler at load to give an initial show/hide state (using a name-spaced event to avoid causing other bound functionality executing).
(Edit: I like what Brandon Boone did regarding keyup for instant action and added a comment to the event name on my code. It will, however, add a little overhead as the function is run for every stroke on the keyboard as opposed to when the field is blurred.)
Demo:
http://jsfiddle.net/JAAulde/fUKfb/3/
Code:
大卫·托马斯的答案当然很有效,我会在简单的情况下使用它。
但是,我一直在开发一个插件,它可以在多个条件和多个元素下执行此操作。我现在想分享这一点。
这是适合您问题的jsfiddle: http://jsfiddle.net/natedavisolds/rVehh/3/ 本质上
,加载插件然后加载:
好处是您可以将reactIf语句链接到一组规则。
这是插件。
David Thomas's answer certainly works well and I would use this for simple cases.
However, I've been working on a plugin that does this with multiple conditions and with multiple elements. I would like to share that now.
Here is the jsfiddle that works for your problem: http://jsfiddle.net/natedavisolds/rVehh/3/
Essentially, load the plugin then on load:
The nice thing is that you can chain the reactIf statement to a set of rules.
Here's the plugin.