在jquery ajax中使用“.attr()”

时间:2013-07-05 09:45:57

标签: jquery ajax post attr

$(".resource_body_build_stone_ok").click(function(){
    $.post("ajax.php",
    {
      build:"resource",
      id: $(this).attr('data-id-build'), 
      level:$(this).attr('data-level-build')
    },
    function(data,status){
        $(".resource_body").load("resource_b.php/?r=1");
        $(".frame_stock").load("stock.php");
        alert("Data: " + data + "\nStatus: " + status);
    });
});

如果值id: $(this).attr('data-id-build')为1到10,我如何在resource_b.php/?r=1中设置id值而不是值1.

例如,如果$(this).attr('data-id-build')的值= 5 ==> resource_b.php/?r=5

3 个答案:

答案 0 :(得分:2)

$(".resource_body_build_stone_ok").click(function(){
    var id = $(this).attr('data-id-build');
    $.post("ajax.php",
    {
      build:"resource",
      id: $(this).attr('data-id-build'), 
      level:$(this).attr('data-level-build')
    },
    function(data,status){
        $(".resource_body").load("resource_b.php/?r=" + id);
        $(".frame_stock").load("stock.php");
        alert("Data: " + data + "\nStatus: " + status);
    });
 });

答案 1 :(得分:1)

<script>
$(".resource_body_build_stone_ok").click(function(){
    var dID = $(this).attr('data-id-build'); //use this dID where you want use dynamic id
    $.post("ajax.php",
    {
      build:"resource",
      id: dID, 
      level:$(this).attr('data-level-build')
    },
    function(data,status){
        $(".resource_body").load("resource_b.php/?r=" + dID);
        $(".frame_stock").load("stock.php");
        alert("Data: " + data + "\nStatus: " + status);
    });
});
</script>

答案 2 :(得分:1)

请查看示例http://jsfiddle.net/2dJAN/85/

var id_build = $(this).attr('data-id-build')

 $(".resource_body").load("resource_b.php/?r="+id_build);

作为参考,我在示例中提醒url格式。