Jquery 验证点击了哪个图像

发布于 2024-10-22 14:32:56 字数 1071 浏览 2 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏佲洛 2024-10-29 14:32:56
$("img").bind("click", function(){
  var whichImg = $(this).attr("title");
  if( whichImg == "continue" ) {
   // do stuff or submit the form
  } else {
     var txt = "That isnt the continue button! You've clicked the "+ whichImg + " button!";
     $("#txtIconG").val( txt );
  }
  return false;
});
$("img").bind("click", function(){
  var whichImg = $(this).attr("title");
  if( whichImg == "continue" ) {
   // do stuff or submit the form
  } else {
     var txt = "That isnt the continue button! You've clicked the "+ whichImg + " button!";
     $("#txtIconG").val( txt );
  }
  return false;
});
好倦 2024-10-29 14:32:56

为每个图片提供一个 id,然后单击每个 id 即可执行您想要的操作。
我希望 img 的 id 能正常工作,否则为每个 img 放置一个 span 或 div 并给它们 id

$('#target').click(function() {
  alert('Handler for .click() called.');
});

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

$('#target').click(function() {
  alert('Handler for .click() called.');
});
神经大条 2024-10-29 14:32:56

已排序。问题是通过 js onclick 更改值不会触发验证。因此,我删除了 onclicks 并在 jQuery 中添加了:

$('#frmExperiment img').click(function(){
    $('#txtIconG').val($(this).attr('src'));
    $('#txtIconG').keyup();
});

并在自定义规则中:

$.validator.addMethod("iconmatch", function (value, element) {
    return value=='btn1.png';
},
    "That isn't the continue button"
);

因此,在输入上触发 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:

$('#frmExperiment img').click(function(){
    $('#txtIconG').val($(this).attr('src'));
    $('#txtIconG').keyup();
});

and in the custom rule:

$.validator.addMethod("iconmatch", function (value, element) {
    return value=='btn1.png';
},
    "That isn't the continue button"
);

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文