سؤال

I'd like to prevent PJAX clicking of a link unless an input box is keyed up.

Here's the code:

$('a.pjax').pjax({container: '#main_content'}).live('click', function(event){ 
   if(keyed){ 
        console.log('yes, you typed');
   }
   else if(keyed==false){
        console.log('no, please type something');
        event.preventDefault();                                                 
   }
}); 

My problem is that despite the condition being determined correctly, PJAX still loads the page regardless of the preventDefault().

Any thoughts on why this is not working?

هل كانت مفيدة؟

المحلول

It seems the pjax function will always fire with the set-up you have in place.

However, the following should work:

$(document).on('click', 'a.pjax', function (event) {
    if (keyed) {
        console.log('yes, you typed');
        return $.pjax.click(event, '#main_content');
    }
    else {
        console.log('no, please type something');
        return false;
    }
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top