开发者

function pageLoad not working

开发者 https://www.devze.com 2023-03-18 05:47 出处:网络
I have been struggling to get my jquery gritter notification to work after a partial postback, after lots of googling i found many suggestions recommending the pageLoad function. I\'m not sure if I ha

I have been struggling to get my jquery gritter notification to work after a partial postback, after lots of googling i found many suggestions recommending the pageLoad function. I'm not sure if I have implemented it right but now the gritter notification doesn't show at all, even before a partial or full postback.

Can you see where I am going wrong? Javascript/jquery isn't my strong point.

<script type="text/javascript">

    function pageLoad() {
        $(function () {
            $('开发者_如何学C.buy-notify').click(function () {
                var spn = this.attributes.getNamedItem('PartNumber').value;
                $.gritter.add({
                    title: 'Order notification..',
                    text: 'Adding ' + spn + ' to basket',
                    time: 1000
                });
                return true;
            });
        });
    };

</script>

Thanks in advance,

Dave


$(function () { ... }); is called on page load, you don't need to wrap it in a function called pageLoad.

If your .buy-notify elements are added after page load, you might want to use .live('click', function() ... instead of the .click(function() ... binding, since .click only binds to elements visible to jQuery on initial page load.


They may be referring to the jQuery shortcut for a function that runs when the page is fully loaded:

$(function() {
  $('.buy-notify').click(function () {
     var spn = this.attributes.getNamedItem('PartNumber').value;
      $.gritter.add({
      title: 'Order notification..',
      text: 'Adding ' + spn + ' to basket',
      time: 1000
    });
    return true;
  });
});


their is no need to do

  $(function () {
});

in a function you can do this by using onload event on Body tag in HTML like

<body onload="javascript:pageLoad()">

otherwise check pageload in jQuery and bind the event

$(function() {
  $('.buy-notify').click(function () {
     var spn = this.attributes.getNamedItem('PartNumber').value;
      $.gritter.add({
      title: 'Order notification..',
      text: 'Adding ' + spn + ' to basket',
      time: 1000
    });
    return true;
  });
});

the third way a little bit simple that

<a href="javascript:void(0)" onclick="javascript:iamclick(0)"></a>

function iamclick(){
   var spn = this.attributes.getNamedItem('PartNumber').value;
          $.gritter.add({
          title: 'Order notification..',
          text: 'Adding ' + spn + ' to basket',
          time: 1000
        });
        return true;
      });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消