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

並べ替えました。問題は、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"
);

そのため、入力でキーアップイベントを発生させるとうまくいきました。これを行うためのよりエレガントな方法があるかもしれませんが、それは機能しています...

于 2011-03-17T21:58:52.273 に答える
0

各 img に ID を付与し、各 ID をクリックすると、必要なことを実行できます。img shud の id が機能することを願っています。それ以外の場合は、各 img にスパンまたは div を配置し、それらに id を付与します。

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