イベント ハンドラー関数に引数を渡すにはどうすればよいですか? これにより、ページの読み込み時に関数が実行されますが、これは望ましい効果ではありません。いくつかの異なるテキストボックス、ドロップダウンの組み合わせに対して実行するには、このルーチン「validateText」が必要です。テキスト/ドロップダウンの組み合わせごとに 1 つ作成する代わりに、「validateText」を再利用できますか??
//add blur event handler to the textbox with jQuery when the page is finished loading
$(document).ready(function() {
$("#myTextbox").blur(validateText($("#myTextbox"), $("#Select1")));
})
function validateText(textbox, dropdown) {
var message = $("#message");
var isValid = false;
//get the value the user type in
var textboxValue = $(textbox).val();
//get the options from the lookup
var options = $("option", dropdown);
//loop through the options and compare it to "value"
options.each(function() {
var optValue = $(this).val();
if (optValue === textboxValue) {
isValid = true;
}
});
if (!isValid)
message.text(textboxValue + " is not a valid value from the list.");
else
message.text(textboxValue + " is perfectly valid.");
}