未捕获的类型错误:无法读取 null 的属性“on”[重复]

2024-01-11

我收到这个未捕获的类型错误:

我不确定是什么原因造成的,也许与 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 http://drivencarsales.co.uk/book-testdrive

这真的让我很困惑,如果有人能帮助我解决这个问题,我将不胜感激。

谢谢,尼克


在您的网站中,您同时拥有prototype.js and jQuery添加。这$这里调用prototype.js代替jQuery。因此,请将其更改为:

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未捕获的类型错误:无法读取 null 的属性“on”[重复] 的相关文章

随机推荐