最初は、クリックイベントが複数回発生していた問題がありましたが、私は下のコードに示すように、おそらくはunbind()
とone()
を使用してこれを克服できました。ファンクションメモリをクリアする必要があります。 jQueryランニング関数が何度も実行されました
私がここに持っているのは、普段使うことのできるモーダルウィンドウを開くコードです。いくつかのケースでは、パスワードフォームを含むさまざまな用途に使用します。
私はあなたがHTMLを必要としないと思いますので、投稿しません。
ボタン、またはアクションウィンドウが必要とされるようにするとき、私はこのような関数を呼び出す:
showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis)
最初の三つの変数は、ウィンドウがどのように見えるかを決定title
とhtml
内容を決定し、 confirmThis
およびdenyThis
は、この関数を呼び出す直前に設定された関数で、confirm
のウィンドウで、confirm
またはdeny
のボタンが押された場合のアクションを決定します。
security
ウィンドウの場合、確認ボタンは、単純なパスワードフォームを送信し、データベースからユーザーIDを返す「署名する」ボタンに置き換えられます。ユーザーIDが正常に返された場合、スクリプトはプログラムでconfirm
ボタンを押し、モーダルウィンドウの開始時の呼び出しに従って関数を実行します。
私の問題があれば、誤ったパスワードが入力された、またはユーザーがブラウザウィンドウを更新せず、後で、ウィンドウをキャンセルすると、正しいパスワードを再入力し、confirmThis()
関数が2回実行(または何回されていることです間違ったパスワード/キャンセルアクションが実行されたため)。
これは明らかに、毎回confirmThis
機能を「記憶」していることを示しています。私が言ったように、最初は、パスワードの成功の機能がconfirmIt
は二回、one()
の豊富な使用は、これを修正したクリックして
、それは今、間違いなく一度だけconfirmIt
をクリックしているが、それはまだ機能、複数の時間を実行しています。
この機能をクリアして1回だけ行うことができますか?
そこから私は、モーダルウィンドウを呼び出しています関数は次のようになります。
$('#saveDelivery').click(function() {
function confirmIt() {
formData = (JSON.stringify($('#delDetail').serializeObject()));
saveData(formData);
$('#saveDelivery').removeClass('centreLoader');
};
showModalAlert('security', '300px', '185px', 'Security!', 'You need to "Sign" this action.', confirmIt, '');
});
それは単にsaveDelivery
要素のクリックは、confirmThis
関数は、この時点で宣言されていますし、AJAXフォームを送信
実際showModalAlert
機能は以下の通りです:
function showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis) {
// stuff that opens the alert window \\
if (confirmThis == '') {
$('#confirmIt').one('click', function() { $('#closeAlert').one('click').click(); });
} else {
$('#confirmIt').one('click', function() { confirmThis(); $('#closeAlert').one('click').click(); });
};
if (denyThis == '') {
$('#denyIt').one('click', function() { $('#closeAlert').one('click').click(); $('#signIt').unbind(); });
} else {
$('#denyIt').one('click', function() { denyThis(); $('#closeAlert').one('click').click(); $('#signIt').unbind(); });
};
if (type == "confirm") {
$('.closeAlert, .signItForm').hide();
};
if (type == "alert") {
$('.alertConfirm, .signItForm').hide();
};
if (type == "fixedAlert") {
$('.closeAlert, .alertConfirm, .signItForm').hide();
};
if (type == "security") {
$('.signItForm').show();
$('.closeAlert').hide();
$('#confirmIt').hide();
$('#signIt').unbind().fadeTo('fast',1);
};
};
$('#signIt').live('click', function() {
var formData = (JSON.stringify($('.secureSign').serializeObject()));
var signitPwd = $('#signItpwd').val();
var jsonURL = "/jsonout/getdata.aspx?sql=SELECT id, password FROM users WHERE password ='" + signitPwd + "' LIMIT 1&output=json&usedb=new&labelName=any&fileName=";
$.getJSON(jsonURL, function (data) {
if (data.length > 0) {
$('.savingUserID').val(data[0].id);
$('#confirmIt').one('click').click();
$('#signIt').fadeTo('fast', 0);
$('#confirmIt').show();
} else {
$('#signIt').fadeTo('fast', 0);
$('#confirmIt').one('click').show();
$('.closeAlert').show();
$('.alertConfirm, .signItForm').hide();
$('#alertTitle').html("Error!");
$('#alertContent').css({ 'text-align': 'center' }).html("Password Denied");
};
});
});
すごくお世話になりました。 –
他の 'unbind()'参照を全て削除して、今は完全に動作します!:D –
いいですね:)!お役に立てて嬉しいです! – Benno