Jquery 验证点击了哪个图像
我有一个包含 5 个图标的表单,我想验证单击的是哪一个。我想添加一个隐藏的文本框并编写一个规则来检查它的值。这适用于表单提交,但我需要在单击正确的图像时清除错误消息。目前,当 JavaScript 更改文本值时,不会触发验证。有更好的方法吗?
<form name="frmExperiment" id="frmExperiment" action="" method="post">
<img src="btn1.png" width="75" height="75" alt="continue" title="continue" onclick="frmExperiment.txtIconG.value=1" />
<img src="btn2.png" width="75" height="74" alt="information" title="information" onclick="frmExperiment.txtIconG.value=2" />
<img src="btn3.png" width="75" height="82" alt="refresh" title="refresh" onclick="frmExperiment.txtIconG.value=3" />
<img src="btn4.png" width="75" height="75" alt="home" title="home" onclick="frmExperiment.txtIconG.value=4" />
<img src="btn6.png" width="75" height="77" alt="stop" title="stop" onclick="frmExperiment.txtIconG.value=5" />
<input type="text" name="txtIconG" id="txtIconG" />
</form>
和
$.validator.addMethod("iconmatch", function (value, element) {
return value==1;
},
"That isn't the continue button"
);
I have a form with 5 icons and I want to validate which one is clicked. I thought to add a hidden text box and write a rule which checks the value of it. This works on form submit, but I need the error message to be cleared on clicking the correct image. At the moment, the validation is not fired when the text value is changed by javascript. Any better way of doing this?
<form name="frmExperiment" id="frmExperiment" action="" method="post">
<img src="btn1.png" width="75" height="75" alt="continue" title="continue" onclick="frmExperiment.txtIconG.value=1" />
<img src="btn2.png" width="75" height="74" alt="information" title="information" onclick="frmExperiment.txtIconG.value=2" />
<img src="btn3.png" width="75" height="82" alt="refresh" title="refresh" onclick="frmExperiment.txtIconG.value=3" />
<img src="btn4.png" width="75" height="75" alt="home" title="home" onclick="frmExperiment.txtIconG.value=4" />
<img src="btn6.png" width="75" height="77" alt="stop" title="stop" onclick="frmExperiment.txtIconG.value=5" />
<input type="text" name="txtIconG" id="txtIconG" />
</form>
and
$.validator.addMethod("iconmatch", function (value, element) {
return value==1;
},
"That isn't the continue button"
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为每个图片提供一个 id,然后单击每个 id 即可执行您想要的操作。
我希望 img 的 id 能正常工作,否则为每个 img 放置一个 span 或 div 并给它们 id
Give an id to each img and on click of each id you do what ever you want .
I hope id of img shud work other wise put a span or div for each img and give id to them
已排序。问题是通过 js onclick 更改值不会触发验证。因此,我删除了 onclicks 并在 jQuery 中添加了:
并在自定义规则中:
因此,在输入上触发 keyup 事件就完成了这项工作。可能有一种更优雅的方法可以做到这一点,但它正在起作用......
Sorted. The problem was that changing the value by js onclick doesn't fire the validation. So, I removed the onclicks and in jQuery added:
and in the custom rule:
So, firing the keyup event on the input did the job. There may be a more elegant way of doing this, but it's working...