jQuery的 - 使用多个请求的相同功能 [英] Jquery - use the same function for multiple requests

查看:127
本文介绍了jQuery的 - 使用多个请求的相同功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的点击功能,对于一个特定的HTML按钮的工作原理。我想用它在多个按钮,但每个按钮需要不同的变量传递到同一页面。

I have this CLICK function that works for one specific HTML button. I'd like to use it on multiple buttons, but each button needs to pass different variables to the same page.

按钮

<input type="button" id="scs" value="TEST" />

JQUERY

$("#scs").click(function() {
    $("#status").html("<p>Please Wait! TESTING</p>");
$.ajax({
  url: 'go.php?ab=1',
  success: function(data, textStatus, xhr) {
  alert (data);
}
});
});

我怎么能适应这个,所以如果我有4个以上的按键每个按钮使用函数,调用相同的页面,但采用独特的价值?

How can I adapt this so if I have 4 more buttons each button uses the function, calls the same page but uses unique values ?

例如:

<input type="button" id="update" value="update" /> Calls go.php?up=1
<input type="button" id="new" value="new" />  Calls go.php?new=1

等等......

etc...

感谢

推荐答案

给你的按钮相同的类和使用类名称调用code和数据属性添加到每个按钮来获取单独的值。

Give your buttons the same class and call the code using the class name, and add a data attribute to each button to retrieve the seperate values.

试试这个:

<input type="button" class="clickButton" data-value="go.php?up=1" value="Update" />
<input type="button" class="clickButton" data-value="go.php?new=1" value="New" />

$(".clickButton").click(function() {
    $("#status").html("<p>Please Wait! TESTING</p>");

    $.ajax({
        url: $(this).attr("data-value"),
        success: function(data, textStatus, xhr) {
            alert (data);
            $(this).prop('value', 'New Text');
        }
    });
});

这篇关于jQuery的 - 使用多个请求的相同功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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