Javascript 使用id参数刷新Ajax内容

Javascript 使用id参数刷新Ajax内容,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试图创建一个函数,从按钮中获取元素值并创建ajax请求。最后,“id”是空的,因此下面的JS函数应该使用“id=1”,但是当我点击带有值“2”的按钮时,函数应该将“includes/agallery.php?id=2”加载到我的#agallery <script type="text/javascript"> var id = 1; $('#agallery').html('Downloading...'); // Show "Downloading..."

我试图创建一个函数,从按钮中获取元素值并创建ajax请求。最后,“id”是空的,因此下面的JS函数应该使用“id=1”,但是当我点击带有值“2”的按钮时,函数应该将“includes/agallery.php?id=2”加载到我的#agallery

<script type="text/javascript">
    var id = 1;
    $('#agallery').html('Downloading...'); // Show "Downloading..."
    // Do an ajax request
    $.ajax({
      url: "includes/agallery.php?id="+id
    }).done(function(data) { // data what is sent back by the php page
      $('#agallery').html(data); // display data
    });
</script>

<button onClick="" value="2" class="small-btn last"></button>

var-id=1;
$('#agallery').html('下载…');//显示“下载…”
//执行ajax请求
$.ajax({
url:“includes/agallery.php?id=“+id
}).done(函数(数据){//data php页面返回的内容
$('#agallery').html(数据);//显示数据
});

您需要在每次请求之前增加id

<script type="text/javascript">
    var id = 0;
    $('#agallery').html('Downloading...'); // Show "Downloading..."
    // Do an ajax request
    id++;
    $.ajax({
      url: "includes/agallery.php?id="+id
    }).done(function(data) { // data what is sent back by the php page
      $('#agallery').html(data); // display data
    });
</script>

var-id=0;
$('#agallery').html('下载…');//显示“下载…”
//执行ajax请求
id++;
$.ajax({
url:“includes/agallery.php?id=“+id
}).done(函数(数据){//data php页面返回的内容
$('#agallery').html(数据);//显示数据
});
您可能需要这个

 <script type="text/javascript">
   $(".small-btn last").click(function () {
        $('#agallery').html('Downloading...'); // Show "Downloading..."
      // Do an ajax request
    $.ajax({
         url: "includes/agallery.php?id="+$(this).val()
        }).done(function(data) { // data what is sent back by the php page
       $('#agallery').html(data); // display data
        });
});

$(“.small btn last”)。单击(函数(){
$('#agallery').html('Downloading…');//显示“Downloading…”
//执行ajax请求
$.ajax({
url:“includes/agallery.php?id=“+$(this).val()
}).done(函数(数据){//data php页面返回的内容
$('#agallery').html(数据);//显示数据
});
});
  • 创建一个
    doAjax
    函数,该函数接受一个参数
    id
    ,并发出ajax请求
  • 为您的按钮注册一个点击事件监听器(并在html中删除该监听器)
  • 使用
    id
    1进行初始
    doAjax
    调用
  • 这是代码中的外观:

    <script type="text/javascript">
    
        // create a function doAjax
        function doAjax(id) {
            $('#agallery').html('Downloading...'); // Show "Downloading..."
            // Do an ajax request
            $.ajax({
                url: "includes/agallery.php?id="+id
            }).done(function(data) { // data what is sent back by the php page
                $('#agallery').html(data); // display data
            });
        }
    
    
        $(function() {
            // register a click handler
            $("button.small-btn").click(function() {
                var id = $(this).val();
                doAjax(id);
            });
    
            // do initial ajax request (with id 1)
            doAjax(1);
        });
    
    </script>
    

    这是一个有效的

    它只有在我这样做时才有效“你删除了
    onClick=”了吗“
    来自html元素?我再次复制了所有代码,但仍然一无所获。我不明白为什么?我添加了一个演示。请看一看。对不起:/还是没什么。我尝试了不同的方法,用输入按钮,id代替类,但仍然一无所获。另外,在您的演示中,单击按钮后不会发生任何事情。
    <button value="2" class="small-btn last"></button>