上.(咔嚓)不开火

On.(click) not firing

本文关键字:开火 咔嚓      更新时间:2023-09-26

我有一个触发ajax调用的on click函数,单击函数不会触发,我使用了:

.click(function(){

这有效,但我需要按钮能够单击多次,所以我尝试了:

<script type='text/javascript'>
  $(document).ready(function(){
    $("#post").on('click', function(){
      alert("JQuery Running!");
      $.ajax({
        type: "GET",
        url: "do.php",
        data: { c: $(this).attr("value") },
        success: function(html){
          $("#add_err").css('display', 'inline', 'important');
          $("#add_err").html(html);
        }
      });
    });
  });
</script>
<div class='err' id='add_err'></div>
<button id='post' value='1' />Do</button>

我也尝试过:

$(document).on('click', '#post', function(){})

这也不起作用,我的控制台.log总是显示:

Uncaught Typeerror: $(...).on is not a function 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

您确定这是页面上引用的唯一脚本吗? 这听起来很像你在某个地方有 1.7 <。我不知道为什么它会抱怨.on().

如果您使用铬,1) 打开开发工具2)转到"网络"选项卡3) 显示过滤器4) 点击 JS5) 在那里查找 jquery 引用

屏幕操作方法 (stackoverflow.com)

如果您使用旧的 jquery 版本 (<1.7),则可以使用 bind:

http://api.jquery.com/bind/

检查以确保您在页面上引用 jQuery 之后而不是之前放置了点击脚本。