2013-07-05 68 views
0
$(".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使用 「.attr()」 中的jquery AJAX

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

+1

您需要將'id'發送回您的函數並將其從'data'中取出 – putvande

回答

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

請檢查示例http://jsfiddle.net/2dJAN/85/

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

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

僅供參考,我會在示例中提醒網址格式。

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