简体   繁体   English

函数javascript与链接的id无法正常工作

[英]function javascript does not work properly with the id of link

I created confirmation message box using this plugin . 我使用这个插件创建了confirmation message box

<input type="button" id="delete" value="Delete All" />
<input type="button" id="deleteByID" value="Delete Item By ID" />

When I click on each button, it called the callMessageBox() to generate the message box and the id of message box is dynamic by the button name. 当我点击每个按钮时,它调用callMessageBox()来生成消息框,消息框的id是按钮名称动态的。 After message box was call, the actionOnMsgBox executes in order to call the callback function . 在调用message box之后,执行actionOnMsgBox以调用callback function

$('#delete').click(function(){
     callMessageBox('delete','Are you sure to delete all items from your cart?','Yes','No');
});
actionOnMsgBox('delete',deleteAllItemInCart);

$('#deleteByID').click(function () {
    callMessageBox('deleteByID','Are you sure to delete this item from your cart?','Yes','No');
});
 actionOnMsgBox('deleteByID',deleteItemInCart,item1);

These are my functions: 这些是我的功能:

 function messageBox(btnID, heading, confirmMsg, cancelMsg){
   var box = ' <div id="modal"><div id="heading">'+heading+'</div><div id="content"> <a href="#" id="'+ btnID + 'revealYes" class="button green close"><img src="images/tick.png">'+confirmMsg+'</a> <a href="#" id="'+btnID+'revealNo" class="button red close"><img src="images/cross.png">'+cancelMsg+'</a>';
   return box;
 }
 function callMessageBox(btnID, heading, confirmMsg, cancelMsg){
     if($('#modal').length == 0)
         $('body').append(messageBox(btnID,heading,confirmMsg,cancelMsg))
            $('#modal').reveal({
                 animation: 'fade',
                 animationspeed: 320,
                 closeonbackgroundclick: true,
                 dismissmodalclass: 'close'
            });
 }

 function actionOnMsgBox(btnID,callback,item){
     $(document).on('click','#'+ btnID +'revealYes,#'+ btnID +'revealNo',function (e) {
        var choice = $(e.target).attr('id');
        if ($(this).attr('id') == btnID + 'revealYes'){
            if(typeof item === 'undefined'){
                callback();
            }else{
                callback(item);
            }
        } else {
            return false;
        }
    });
 }

Problem : when I clicked on Delete All button, then the messageBox() generated deleterevealYes and deleterevealNo . 问题:当我点击Delete All按钮时, messageBox()生成了deleterevealYesdeleterevealNo But when I clicked on Delete Item By ID the messageBox() still generated #deleterevealYes and #deleterevealNo , what I expected, it should be #deleteByIDrevealYes and #deleteByIDrevealYes . 但是当我点击按Delete Item By IDmessageBox()仍然生成了#deleterevealYes#deleterevealNo ,我的预期,应该是#deleteByIDrevealYes#deleteByIDrevealYes

Any idea what could be causing this. 任何想法可能导致这种情况。 Thanks. 谢谢。

Change the below functions. 更改以下功能。 Give unique id for reveal function. 为揭示功能提供唯一ID。 If same id is given, the first one will be executed always 如果给出相同的id,则将始终执行第一个id

function messageBox(btnID, heading, confirmMsg, cancelMsg){
   var box = ' <div id="modal'+btnID+'"><div id="heading">'+heading+'</div><div id="content"> <a href="#" id="'+ btnID + 'revealYes" class="button green close"><img src="images/tick.png">'+confirmMsg+'</a> <a href="#" id="'+btnID+'revealNo" class="button red close"><img src="images/cross.png">'+cancelMsg+'</a>';
   return box;
 }

 function callMessageBox(btnID, heading, confirmMsg, cancelMsg){
     if($('#modal'+btnID).length == 0)
         $('body').append(messageBox(btnID,heading,confirmMsg,cancelMsg))
            $('#modal'+btnID).reveal({
                 animation: 'fade',
                 animationspeed: 320,
                 closeonbackgroundclick: true,
                 dismissmodalclass: 'close'
            });
 }

我相信如果($('#modal')。length == 0)导致你的问题你只为所有元素附加一个div并跳过特定ID的那个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM