如何运行jQuery onClick?需要传递一个变量来运行.ajax [英] How to run jQuery onClick? Need to pass a variable to run .ajax

查看:75
本文介绍了如何运行jQuery onClick?需要传递一个变量来运行.ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行.ajax,并从页面的onClick项中插入一个数据元素。



像这样的东西:

>函数grabinfo(foo){

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

}

< input onClick =javascript:grabinfo(18343)/>
//在页面上,每个项目都有这个按钮输入


解决方案

 < input type =按钮value =18434/> 

在您的JavaScript文件中:

<$ p $点击(功能(){
$ .ajax({
url:'infospitter.php',$ b $('input [type = button]') b方法:'GET',
data:'id ='+ $(this).attr('value'),
success:function(returnData){
//做某事returnData
}
});
});


I'm trying to run .ajax and insert a data element from the onClick of an item from the page. Whats the best way to do this?

Something like this:

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

解决方案

It's usually best to keep your javascript unobtrusive, and out of the DOM.

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

In your javascript file:

$('input[type=button]').click(function(){
    $.ajax({
        url: 'infospitter.php',
        method: 'GET',
        data: 'id=' + $(this).attr('value'),
        success: function(returnData){
            // Do something with returnData
        }
    });
});

这篇关于如何运行jQuery onClick?需要传递一个变量来运行.ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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