未捕获的TypeError:无法读取null的属性"on" [英] Uncaught TypeError: Cannot read property 'on' of null

查看:348
本文介绍了未捕获的TypeError:无法读取null的属性"on"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这个Uncaught TypeError:

I'm getting this Uncaught TypeError:

我不确定是什么原因造成的,也许与JQuery有关?

I'm unsure what is causing it, maybes something to do with JQuery?

这是确切的代码:

//when add to cart link is clicked...
$('.addtocart').on('click', function(){

  //get the value of the "data-value" attribute for that link
  var vehicleRegistration = $(this).data('value');
  //save it to localStorage
  localStorage.setItem('vehicleRegistration', vehicleRegistration);


  //read from localStorage
  if( localStorage.getItem('vehicleRegistration') ){
    //add the value to the form input
    $('#field34').val( localStorage.getItem('vehicleRegistration') );
  }

});

这是它发生在的页面: http://drivencarsales.co.uk/book -testdrive

Here is the page that it is happening on: http://drivencarsales.co.uk/book-testdrive

这真的让我感到难过,如果有人可以帮助我解决这个问题,将不胜感激.

It's really got me stumped, if anyone can help me solve this it would be much appreciated.

谢谢尼克

推荐答案

在您的站点中,您同时添加了prototype.jsjQuery.此处的$调用prototype.js而不是jQuery.因此,请将该代码更改为:

In your site, you have both prototype.js and jQuery added. The $ here calls prototype.js instead of jQuery. So instead of that code, please change it to:

jQuery('.addtocart').on('click', function(){

  //get the value of the "data-value" attribute for that link
  var vehicleRegistration = jQuery(this).data('value');
  //save it to localStorage
  localStorage.setItem('vehicleRegistration', vehicleRegistration);


  //read from localStorage
  if( localStorage.getItem('vehicleRegistration') ){
    //add the value to the form input
    jQuery('#field34').val( localStorage.getItem('vehicleRegistration') );
  }

});

或者您也可以这样做:

(function ($) {
    $('.addtocart').on('click', function(){

      //get the value of the "data-value" attribute for that link
      var vehicleRegistration = $(this).data('value');
      //save it to localStorage
      localStorage.setItem('vehicleRegistration', vehicleRegistration);


      //read from localStorage
      if( localStorage.getItem('vehicleRegistration') ){
        //add the value to the form input
        $('#field34').val( localStorage.getItem('vehicleRegistration') );
      }

    });
})(jQuery);

这篇关于未捕获的TypeError:无法读取null的属性"on"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆