Jquery函数不适用于两个或多个窗体
Jquery function not working for two or more forms
大家好,我有下面的jquery函数,只有当输入有1个或多个值时才启用提交,但我的页面上有同样多的输入和提交按钮,jquery函数对第一个表单很好,但对其他表单不起作用。
脚本:
$(function ()
{
$("#textbox").bind("change keyup", function ()
{
if ($("#textbox").val() != "")
$(this).closest("form").find(":submit").removeAttr("disabled");
else
$(this).closest("form").find(":submit").attr("disabled", "disabled");
});
});
HTML:
<form action="http://localhost/" method="post" accept-charset="utf-8">
<input type="text" id="textbox" name="textbox" />
<input type="submit" id="submit" name="submit" value="textbox" disabled="disabled" />
</form>
<form action="http://localhost/" method="post" accept-charset="utf-8">
<input type="text" id="textbox" name="textbox" />
<input type="submit" id="submit" name="submit" value="textbox" disabled="disabled" />
</form>
<form action="http://localhost/" method="post" accept-charset="utf-8">
<input type="text" id="textbox" name="textbox" />
<input type="submit" id="submit" name="submit" value="textbox" disabled="disabled" />
</form>
当我在第一个文本框中键入任何内容时,提交按钮会被激活并正常工作,但如果我在第二个或第三个文本框添加任何值,提交按钮不会被激活。
因为在同一页面上只能添加一个id="textbox"的元素。
在输入中添加一些类名(例如"textbox"(,并在事件绑定上将选择器"#textbox"更改为".textbox":
$(function ()
{
$(".textbox").bind("change keyup", function ()
{
if ($(this).val() != "")
$(this).closest("form").find(":submit").removeAttr("disabled");
else
$(this).closest("form").find(":submit").attr("disabled", "disabled");
});
});
只需使用class
而不是id
,因为不能有多个元素具有相同的id
。
此外,您需要在事件处理程序中使用$(this)
来访问更改后的元素。
试试这个:
<script type="text/javascript">
$(function () {
$(".textbox").bind("change keyup", function () {
if ($(this).val() != "")
$(this).closest("form").find(":submit").removeAttr("disabled");
else
$(this).closest("form").find(":submit").attr("disabled", "disabled");
});
});
</script>
<form action="http://localhost/" method="post" accept-charset="utf-8">
<input type="text" class="textbox" name="textbox" />
<input type="submit" class="submit" name="submit" value="textbox" disabled="disabled" />
</form>
<form action="http://localhost/" method="post" accept-charset="utf-8">
<input type="text" class="textbox" name="textbox" />
<input type="submit" class="submit" name="submit" value="textbox" disabled="disabled" />
</form>
<form action="http://localhost/" method="post" accept-charset="utf-8">
<input type="text" class="textbox" name="textbox" />
<input type="submit" class="submit" name="submit" value="textbox" disabled="disabled" />
</form>
首先,在html
中使用相同的ID's
来获得更多的div
,这并不好。Id是唯一的。请改用class
。否则它也不能与classes
一起工作,您应该尝试使用3个不同的div或$.each()
函数,或者在keyup()
上进行识别
相关文章:
- JQuery合并了keyup和focusout两个函数
- 如何使用 node.js 比较两个 json 数组
- 为复选框javascript指定两个值
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 单击时切换两个图像
- 我可以'我似乎不知道如何修复javascript中的两个lint.有人能帮我理解吗
- 基于两个条件退出While循环
- 如何在这里将两个值最低的数字相加
- 组合两个javascript函数
- 如何使用offer/answer交换来自两个对等连接的流
- jsf中两个字符串的颜色代码差异
- 加载两个具有相同父密钥名称的json文件
- 在Qualtrics中,介绍如何动态连接两个滑块
- 访问$.ajax()函数中的两个不同数组
- 如何在three.js上添加两个向量
- 同时在窗体中显示两个 JSON 值
- 仅执行的第一个函数-由两个窗体的.osubmit调用的两个函数中的一个
- 两个按钮一个窗体
- 选择窗体中作用于两个不同下拉框的所有复选框
- Jquery函数不适用于两个或多个窗体