将参数传递给JQuery函数 [英] Passing parameters to a JQuery function

查看:113
本文介绍了将参数传递给JQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个包含Action列的循环创建HTML。该列
是一个超链接,当用户点击调用JavaScript
函数并传递参数时...



示例:

 < a href =#OnClick =DoAction(1,'Jose'); >点击< / a> 
< a href =#OnClick =DoAction(2,'Juan'); >点击< / a>
< a href =#OnClick =DoAction(3,'Pedro'); >点击< / a>
...
< a href =#OnClick =DoAction(n,'xxx'); >点击< / a>

我希望该函数用正确的
参数调用Ajax jQuery函数。 p>

任何帮助?

解决方案

使用POST

 函数DoAction(id,name)
{
$ .ajax({
type:POST,
url:someurl.php,
data:id =+ id +& name =+ name,
success:function(msg){
alert(保存的数据:+ msg);
}
});

$ / code>

使用GET

函数DoAction(id,name)
{
$ .ajax({
类型:GET,
url:someurl。 php,
data:id =+ id +& name =+ name,
success:function(msg){
alert(Data Saved:+ msg) ;
}
});

编辑:

也许,更好的方式来做到这一点,如果没有启用JavaScript,可以使用GET(使用GET)来生成href的URL,然后使用单击处理程序通过ajax调用该URL。

 < a href =/ someurl.php?id = 1& name = Joseclass =ajax-link>点击< / a> 
< a href =/ someurl.php?id = 2& name = Juanclass =ajax-link>点击< / a>
< a href =/ someurl.php?id = 3& name = Pedroclass =ajax-link>点击< / a>
...
< a href =/ someurl.php?id = n& name = xxxclass =ajax-link>点击< / a>

< script type =text / javascript>
$(function(){
$('。ajax-link')。click(function(){
$ .get($(this).attr('href'),函数(msg){
alert(Data Saved:+ msg);
});
返回false; //不要跟随链接!
});
});
< / script>


I'm creating HTML with a loop that has a column for Action. That column is a Hyperlink that when the user clicks calls a JavaScript function and passes the parameters...

example:

<a href="#" OnClick="DoAction(1,'Jose');" > Click </a>
<a href="#" OnClick="DoAction(2,'Juan');" > Click </a>
<a href="#" OnClick="DoAction(3,'Pedro');" > Click </a>
...
<a href="#" OnClick="DoAction(n,'xxx');" > Click </a>

I want that function to call an Ajax jQuery function with the correct parameters.

Any help?

解决方案

Using POST

function DoAction( id, name )
{
    $.ajax({
         type: "POST",
         url: "someurl.php",
         data: "id=" + id + "&name=" + name,
         success: function(msg){
                     alert( "Data Saved: " + msg );
                  }
    });
}

Using GET

function DoAction( id, name )
{
     $.ajax({
          type: "GET",
          url: "someurl.php",
          data: "id=" + id + "&name=" + name,
          success: function(msg){
                     alert( "Data Saved: " + msg );
                   }
     });
}

EDIT:

A, perhaps, better way to do this that would work (using GET) if javascript were not enabled would be to generate the URL for the href, then use a click handler to call that URL via ajax instead.

<a href="/someurl.php?id=1&name=Jose" class="ajax-link"> Click </a>
<a href="/someurl.php?id=2&name=Juan" class="ajax-link"> Click </a>
<a href="/someurl.php?id=3&name=Pedro" class="ajax-link"> Click </a>
...
<a href="/someurl.php?id=n&name=xxx" class="ajax-link"> Click </a>

<script type="text/javascript">
$(function() {
   $('.ajax-link').click( function() {
         $.get( $(this).attr('href'), function(msg) {
              alert( "Data Saved: " + msg );
         });
         return false; // don't follow the link!
   });
});
</script>

这篇关于将参数传递给JQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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