我正在尝试运行 .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
最佳答案
通常最好保持 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
}
});
});
关于javascript - 如何运行 jQuery onClick?需要传递一个变量来运行.ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495692/