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"
);
4

3 回答 3

0
$("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;
});
于 2011-03-17T20:04:31.773 回答
0

为每个 img 提供一个 id,然后单击每个 id,您可以随心所欲。我希望 img shud 的 id 工作,否则为每个 img 放置一个 span 或 div 并给他们 id

$('#target').click(function() {
  alert('Handler for .click() called.');
});
于 2011-03-17T20:06:49.090 回答
0

排序。问题是通过 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 事件就可以完成这项工作。可能有一种更优雅的方式来做到这一点,但它正在工作......

于 2011-03-17T21:58:52.273 回答