duananyu9231 2014-01-27 10:59
浏览 59
已采纳

Jquery - 对多个请求使用相同的函数

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.

BUTTON

<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);
}
});
});

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 ?

EG :

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

Thanks

  • 写回答

5条回答 默认 最新

  • duanchuo7741 2014-01-27 11:04
    关注

    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.

    Give this a try:

    <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');
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了