0

I've voting mechanism in my website, if user tries to vote(up or down) I'll check whether he logged-in or not, for that I've written following code,

      $(".vote").click(function(){
         if(is_logged_in)
          {
             // Doing necessary stuff for vote up/down
          }else
            {
             // Showing Login Dialog box                        
            submitForm(".lform",function(response){

             if(data.status == "1")
               {
                 //Closing the Login Dialog Box
                 is_logged_in = 1;  // Assigning the value
                 bindEvent(".vote","click");  // Binding the Event,but can't able to it
               }
              });//Form Submission Closed
            }// Else Closed
         });// Main 'click' closed

     function bindEvent(selector,eventType){
         $(selector).bind(eventType);
        }

Instead of external function bindEvent(".vote","click") I've tried without the bindEvent(), but I can't able to bind the click event dynamically after successful login.

4

2 回答 2

1

You should use on() for binding dynamically created elements.

 $(document).on("click", ".vote",function(){
      if(is_logged_in)
      {
         // Doing necessary stuff for vote up/down
      }
      else
      {
         // Showing Login Dialog box                        
        submitForm(".lform",function(response){

         if(data.status == "1")
           {
             //Closing the Login Dialog Box
             is_logged_in = 1;  // Assigning the value
             bindEvent(".vote","click");  // Binding the Event,but can't able to it
           }
          });//Form Submission Closed
        }// Else Closed
});// Main 'click' closed
于 2013-04-13T09:41:09.217 回答
1

In order to bind something to occur on an event you need to pass a callback to be triggered when that event occurs. In your code above you aren't specifying a callback.

function bindEvent(selector,eventType){
  $(selector).bind(eventType);
}

You need something like this:

function bindEvent(selector,eventType,callBack){
  $(selector).bind(eventType, callBack);
}

Which you would use like:

bindEvent('.target', 'click', function(){
  alert('This will trigger on click!');
})

update

After re-reading your code it looks to me like what you actually need is to trigger the click event, rather than bind something to it:

$(".vote").click(function(){
  var $vote = $(this);
  if(is_logged_in) {
    // Doing necessary stuff for vote up/down
  }
  else {
    // Showing Login Dialog box                        
    submitForm(".lform",function(response){
      if(data.status == "1"){
        //Closing the Login Dialog Box
        is_logged_in = 1;  // Assigning the value
        $vote.click();
      }
    });//Form Submission Closed
  }// Else Closed
});

Using click() is the simple method, or you could use jQuery's .trigger('click') method. You may also wish to avoid the click event bubbling up to parent elements, which in this case would make sense, so you could also use .triggerHandler('click') instead.

于 2013-04-13T09:44:50.143 回答