تأكيد وظيفة زمنية واحدة فقط مع jQuery
سؤال
لديّ هذه الوظيفة التي ستعمل على خلط قائمة من الصور ، عندما أضغط على زر #Shuffle ، أريد تشغيل رسالة ، إذا تم تأكيدها في المرة الثانية ، فلن أرغب في السؤال مرة أخرى!
كيف ترمز هذا؟
$('#shuffle').click(function(){
if(confirm('This will shuffle all your bla bla bla')){
$("#upimagesQueue li").shuffle();
}
});
المحلول
يمكنك استخدام متغير ، مثل هذا:
var confirmed = false;
$('#shuffle').click(function(){
if(confirmed || confirm('This will shuffle all your bla bla bla')){
$("#upimagesQueue li").shuffle();
confirmed = true;
}
});
يبدأ هذا بتأكيد عدم الاعتراف به ، ولكن في المرة الأولى التي يتم فيها ، يتم تعيينه على صحيح. هذا يعني الجزء الأول من condition1 OR condition2
في الخاص بك if()
سيكون صحيحًا ، لذا فإن التأكيد لن يظهر مرة أخرى.
بدلاً من ذلك ، يمكنك استخدام .data()
لتخزين المتغير لهذا:
$('#shuffle').click(function(){
if($(this).data('confirmed') || confirm('This will shuffle all your bla bla bla')){
$(this).data('confirmed', true);
$("#upimagesQueue li").shuffle();
}
});
نصائح أخرى
هذه طريقة أفضل للقراءة
$('#shuffle').bind('click', function(event){
if(confirm('are you sure?')){
$(this).unbind(event);
$('#shuffle').bind('click', function(){
$("#upimagesQueue li").shuffle();
});
$("#upimagesQueue li").shuffle();
}
});
نرى: http://api.jquery.com/one/
يمكن أيضًا كتابتها على النحو التالي:
$('#shuffle').bind('click', function(event){
if(confirm('are you sure?')){
$(this).unbind(event).click(function(){
$("#upimagesQueue li").shuffle();
})
$("#upimagesQueue li").shuffle();
}
});
لا تنتمي إلى StackOverflow