Javascript 如何运行jqueryonclick?需要传递一个变量来运行.ajax

Javascript 如何运行jqueryonclick?需要传递一个变量来运行.ajax,javascript,function,jquery,onclick,Javascript,Function,Jquery,Onclick,我正在尝试运行.ajax并从页面中的一个项的onClick中插入一个数据元素。最好的方法是什么 大概是这样的: function grabinfo(foo){ $.ajax({ url: "infospitter.php", method: "GET", data: "id="+foo, success: function(html){ $(#showstuff).html(html); } }

我正在尝试运行.ajax并从页面中的一个项的onClick中插入一个数据元素。最好的方法是什么

大概是这样的:

function grabinfo(foo){

      $.ajax({
      url: "infospitter.php",
      method: "GET",
      data: "id="+foo,
      success: function(html){
       $(#showstuff).html(html);
       }
      });

}

  <input onClick="javascript:grabinfo(18343)" />
// and on page each item will have this button input
函数grabinfo(foo){
$.ajax({
url:“infospitter.php”,
方法:“获取”,
数据:“id=”+foo,
成功:函数(html){
$(#showtuff).html(html);
}
});
}
//在页面上,每个项目都有此按钮输入

从onclick属性中删除“javascript:”。只有在javascript事件属性之外运行代码时才需要这样做。

通常最好保持javascript不引人注目,并且远离DOM

<input type="button" value="18434"/>

这不是问题。JavaScript支持块标记,因此可以正常运行。
$('input[type=button]').click(function(){
    $.ajax({
        url: 'infospitter.php',
        method: 'GET',
        data: 'id=' + $(this).attr('value'),
        success: function(returnData){
            // Do something with returnData
        }
    });
});