Jquery 将ajax url替换为具有数据属性的已单击元素

Jquery 将ajax url替换为具有数据属性的已单击元素,jquery,ajax,html,custom-data-attribute,Jquery,Ajax,Html,Custom Data Attribute,我的问题对于jquery爱好者来说很简单,但对我来说很难。我有一个简单的a href标记,其中包含数据属性 <a href="#/one" data-menu-button="menu1" class="button">menu 1</a> <a href="#/two" data-menu-button="menu2" class="button">menu 2</a> // etc 我的问题: 如何用单击的按钮数据属性替换ajax url?

我的问题对于jquery爱好者来说很简单,但对我来说很难。我有一个简单的
a href
标记,其中包含数据属性

<a href="#/one" data-menu-button="menu1" class="button">menu 1</a>
<a href="#/two" data-menu-button="menu2" class="button">menu 2</a>

// etc
我的问题:

如何用单击的按钮数据属性替换ajax url?例如,正如您所看到的,目前我有
url:“test.php”
。我需要的是将
test
更改为数据属性(例如:
menu1
),但保留.php扩展名。我需要该脚本查找单击的数据属性,并将其替换为
test
。也许
$(这个)
能用吗

感谢您的回答,抱歉英语不好。

您可以使用从数据属性中获取值

使用
$(this).data('menu-button')
数据菜单按钮
获取数据,然后将
.php
附加到该按钮上。它看起来像
menu1.php
<代码>$(此)将引用单击的当前元素

您还必须使用
e.preventDefault()
停止页面导航到href属性中的任何内容。除非你想在url页面上找到这些片段

$(".button").click(function(e){
    e.preventDefault();
    $.ajax({url: $(this).data('menu-button') + '.php', success: function(result){
       $(".page").html(result);
    }});
});
您可以使用从数据属性中获取值

使用
$(this).data('menu-button')
数据菜单按钮
获取数据,然后将
.php
附加到该按钮上。它看起来像
menu1.php
<代码>$(此)将引用单击的当前元素

您还必须使用
e.preventDefault()
停止页面导航到href属性中的任何内容。除非你想在url页面上找到这些片段

$(".button").click(function(e){
    e.preventDefault();
    $.ajax({url: $(this).data('menu-button') + '.php', success: function(result){
       $(".page").html(result);
    }});
});
您可以使用从数据属性中获取值

使用
$(this).data('menu-button')
数据菜单按钮
获取数据,然后将
.php
附加到该按钮上。它看起来像
menu1.php
<代码>$(此)将引用单击的当前元素

您还必须使用
e.preventDefault()
停止页面导航到href属性中的任何内容。除非你想在url页面上找到这些片段

$(".button").click(function(e){
    e.preventDefault();
    $.ajax({url: $(this).data('menu-button') + '.php', success: function(result){
       $(".page").html(result);
    }});
});
您可以使用从数据属性中获取值

使用
$(this).data('menu-button')
数据菜单按钮
获取数据,然后将
.php
附加到该按钮上。它看起来像
menu1.php
<代码>$(此)将引用单击的当前元素

您还必须使用
e.preventDefault()
停止页面导航到href属性中的任何内容。除非你想在url页面上找到这些片段

$(".button").click(function(e){
    e.preventDefault();
    $.ajax({url: $(this).data('menu-button') + '.php', success: function(result){
       $(".page").html(result);
    }});
});

正如您所猜测的,使用
$(this)
访问元素,然后使用
.attr('data-menu-button)
获取属性值

$(".button").click(function(){
    var url = $(this).attr('data-menu-button') + '.php';
    $.ajax({url: url, success: function(result){
       $(".page").html(result);
    }});
})

正如您所猜测的,使用
$(this)
访问元素,然后使用
.attr('data-menu-button)
获取属性值

$(".button").click(function(){
    var url = $(this).attr('data-menu-button') + '.php';
    $.ajax({url: url, success: function(result){
       $(".page").html(result);
    }});
})

正如您所猜测的,使用
$(this)
访问元素,然后使用
.attr('data-menu-button)
获取属性值

$(".button").click(function(){
    var url = $(this).attr('data-menu-button') + '.php';
    $.ajax({url: url, success: function(result){
       $(".page").html(result);
    }});
})

正如您所猜测的,使用
$(this)
访问元素,然后使用
.attr('data-menu-button)
获取属性值

$(".button").click(function(){
    var url = $(this).attr('data-menu-button') + '.php';
    $.ajax({url: url, success: function(result){
       $(".page").html(result);
    }});
})

也要感谢你,但是@Dhiraj-Bodicherla有了很好的解释就更快了:)也要感谢你,但是@Dhiraj-Bodicherla有了很好的解释就更快了:)也要感谢你,但是@Dhiraj-Bodicherla有了很好的解释就更快了:)