如何使用 jQuery 检查所有表单字段是否包含值
我需要知道是否所有字段都有值,如果有,我想激活提交按钮。
我看过插件,但我不知道我需要/应该使用哪一个。
该表单使用输入
、文本框
和下拉选择
。
I need away to know if all fields have a value and if they do, I want to activate the submit button.
I have looked at plugins but I don't know which one I need/should use.
The form uses input
, textbox
and dropdown selects
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你不想使用插件,你可以做一些简单的事情,比如这样(注意:我还没有测试过这个):
然后在某些事件上(可能是
focus
或blur) 您调用此函数,并根据返回值切换提交按钮。
If you don't want to use a plugin you could do something simple like this (note: I haven't tested this):
and then on some event (probably
focus
orblur
) you call this function, and toggle the submit button based on the return value.你可以使用jquery通过控件的id来选择你想要的控件。我建议这样做而不是必须支持插件——这对你来说本质上可能是一个黑匣子。
标记:
jquery:
您可以根据有效启用/禁用提交按钮。
You can use jquery to select the controls you want via their id. I would suggest this over having to support a plugin--which could essentially be a black box to you.
markup:
jquery:
You can enable / disable your submit button based on valid.
这会将事件处理程序绑定到
change
事件的输入表单,该事件检查表单中每个输入的值,如果有空白,则禁用提交按钮。它还会将任何空白输入的背景颜色
更改为红色
。这是一个演示: http://jsfiddle.net/2zjbM/1/
This binds an event handler to the inputs a form for the
change
event that checks the value of each input in the form, if any are blank then the submit button is disabled. It also changes thebackground-color
of any blank inputs tored
.Here is a demo: http://jsfiddle.net/2zjbM/1/