将参数传递给JQuery函数

时间:2022-12-05 19:16:55

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...

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

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.

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

Any help?

有帮助吗?

5 个解决方案

#1


83  

Using POST

使用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

使用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.

如果没有启用javascript,也许更好的方法(使用GET)可以生成href的URL,然后使用单击处理程序通过ajax调用该URL。

<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>

#2


4  

If you want to do an ajax call or a simple javascript function, don't forget to close your function with the return false

如果你想做一个ajax调用或一个简单的javascript函数,不要忘记用return false关闭你的函数

like this:

喜欢这个:

function DoAction(id, name) 
{ 
    // your code
    return false;
}

#3


0  

Do you want to pass parameters to another page or to the function only?

是否要将参数传递给其他页面或仅传递给函数?

If only the function, you don't need to add the $.ajax() tvanfosson added. Just add your function content instead. Like:

如果只是函数,则不需要添加$ .ajax()tvanfosson。只需添加您的功能内容即可。喜欢:

function DoAction (id, name ) {
    // ...
    // do anything you want here
    alert ("id: "+id+" - name: "+name);
    //...
}

This will return an alert box with the id and name values.

这将返回一个包含id和name值的警告框。

#4


0  

try something like this

尝试这样的事情

#vote_links a will catch all ids inside vote links div id ...

#vote_links a将捕获所有内部投票链接div id ...

<script type="text/javascript">

  jQuery(document).ready(function() {
  jQuery(\'#vote_links a\').click(function() {// alert(\'vote clicked\');
    var det = jQuery(this).get(0).id.split("-");// alert(jQuery(this).get(0).id);
    var votes_id = det[0];


   $("#about-button").css({
    opacity: 0.3
   });
   $("#contact-button").css({
    opacity: 0.3
   });

   $("#page-wrap div.button").click(function(){

#5


0  

<script type="text/javascript" src="jquery.js">
</script>

 <script type="text/javascript">

  function omtCallFromAjax(urlVariable)
{ 
    alert("omt:"+urlVariable);
     $("#omtDiv").load("omtt.php?"+urlVariable);
}

 </script>

try this it work for me

试试这个对我有用

#1


83  

Using POST

使用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

使用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.

如果没有启用javascript,也许更好的方法(使用GET)可以生成href的URL,然后使用单击处理程序通过ajax调用该URL。

<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>

#2


4  

If you want to do an ajax call or a simple javascript function, don't forget to close your function with the return false

如果你想做一个ajax调用或一个简单的javascript函数,不要忘记用return false关闭你的函数

like this:

喜欢这个:

function DoAction(id, name) 
{ 
    // your code
    return false;
}

#3


0  

Do you want to pass parameters to another page or to the function only?

是否要将参数传递给其他页面或仅传递给函数?

If only the function, you don't need to add the $.ajax() tvanfosson added. Just add your function content instead. Like:

如果只是函数,则不需要添加$ .ajax()tvanfosson。只需添加您的功能内容即可。喜欢:

function DoAction (id, name ) {
    // ...
    // do anything you want here
    alert ("id: "+id+" - name: "+name);
    //...
}

This will return an alert box with the id and name values.

这将返回一个包含id和name值的警告框。

#4


0  

try something like this

尝试这样的事情

#vote_links a will catch all ids inside vote links div id ...

#vote_links a将捕获所有内部投票链接div id ...

<script type="text/javascript">

  jQuery(document).ready(function() {
  jQuery(\'#vote_links a\').click(function() {// alert(\'vote clicked\');
    var det = jQuery(this).get(0).id.split("-");// alert(jQuery(this).get(0).id);
    var votes_id = det[0];


   $("#about-button").css({
    opacity: 0.3
   });
   $("#contact-button").css({
    opacity: 0.3
   });

   $("#page-wrap div.button").click(function(){

#5


0  

<script type="text/javascript" src="jquery.js">
</script>

 <script type="text/javascript">

  function omtCallFromAjax(urlVariable)
{ 
    alert("omt:"+urlVariable);
     $("#omtDiv").load("omtt.php?"+urlVariable);
}

 </script>

try this it work for me

试试这个对我有用