onClickコードをonSumbitコードと同じ関数に配置します。
アップデート
onClick
コードの最後でreturn false;
、これによりイベントの通常の伝播が停止し、onSubmit
イベントの発生が停止します。したがって、送信ボタンでフォームを送信する場合は、そのハンドラーreturn false;
から削除します。onClick
送信ボタンをクリックするclick
と、ボタンでイベントが発生submit
し、ボタンがネストされているフォームでイベントが発生します(のようなものでイベントの伝播を停止しない限りreturn false;
)。
したがって、実際submit
には、現在の両方のハンドラーの役割を実行するイベントハンドラーのみが必要です。
また、ページにjQuery Coreが含まれているように見えるので、次のようなイベントハンドラーをアタッチできます。
$(function () {
$('#form-id').on('submit', function () {
var $this = $(this);//$this refers to the form that is being submitted
jQuery.facebox({
ajax : 'wishlist.php?emailme=true&name=' + $this.find('#name').val() + '&country=' + $this.find('#country').val() + '&email=' + $this.find('#email').val() + '&department=' + $this.find('#department').val()
});
//now we run your normal onSubmit code and return it's return value of this event handler
return formCheck(this);
});
});
フォーム全体をjQuery.facebox
関数に送信する場合は、jQueryの.serialize()
関数を使用して必要なクエリ文字列を作成できます。
$(function () {
$('#form-id').on('submit', function () {
jQuery.facebox({
ajax : 'wishlist.php?' + $(this).serialize()
});
return formCheck(this);
});
});
これがデモです:http://jsfiddle.net/vAFfj/
ドキュメント.serialize()
:http ://api.jquery.com/serialize
.on()
これはjQuery1.7の新機能であり、この場合は.bind()
古いバージョンと同じであることに注意してください。
アップデート
formCheck()
プラグインを実行する前に関数からの戻り値を確認したい場合は、次のfacebox
ようにすることができます。
$(function () {
$('#form-id').on('submit', function () {
//check if the form data is valid
if (formCheck(this) === true) {
//if the form data is valid then run the facebox plugin
jQuery.facebox({
ajax : 'wishlist.php?' + $(this).serialize()
});
//also return true to stop running this function
return true;
}
//if the form data is not valid then return false to stop the submission of the form
return false;
});
});