jQuery 検証プラグインを使用してフォームを検証しています。さらに、入力フィールドの 1 つをぼかして、別のフォーム要素 (選択リスト) を変更する関数を実行したいのですが、入力フィールドが有効な場合に限ります。
だから私のjqueryコードは次のようなものです:
$(document).ready(function()
{
$("#form_name").validate(
{rules:
{
input_field_name: {remote: validation_url}
,input_field_name2: {remote: validation_url2}
}
,messages:
{
input_field_name: {remote: jQuery.format("Error message")}
,input_field_name2: {remote: jQuery.format("Different error message")}
}
});
$("#input_field_name").blur(function()
{
if ($("#input_field_name").valid())
{$.ajax({dataType: "json"
,url: "url_to_get_new_select_list_options"
,data: {input_field_name : $("#input_field_name").val()}
,success: function(data)
{
$select_options = $("#example_select_list");
$select_options.empty();
$.each(data.tp, function(key, val)
{$select_options.append('<option id="'+val.id+'">'+val.desc+'</option>')}
)
}
})
}
});
});
そして、私のhtmlは次のようなものです:
<form name="form_name" id="form_name" method="post" action="url_to_process_form">
<select name="example_select_list" id="example_select_list">
<option value="">Please select...</option>
<option value="11111">One</option>
<option value="22222">Two</option>
<option value="33333">Three</option>
<option value="44444">Four</option>
<option value="55555">Five</option>
</select>
<input id="input_field_name" type="text" size=10 maxlength=20 name="input_field_name2">
<input id="input_field_name2" type="text" size=10 maxlength=20 name="input_field_name2">
</form>
私が知る限り、問題は検証前にぼかし関数が実行されていることです。validは false を返すため、私のぼかし関数は何も役に立ちません。(値を変更せずに) フィールドをもう一度ぼかすと、. validは true を返し、選択リストは必要に応じて更新されます。
しかし、フォーム(またはその特定の入力フィールドのみ)を検証する方法がわかりません。validは、blur 関数の最初のパスで true を返します。
そのような初心者の質問で申し訳ありません。