doulidai6316 2016-02-07 17:34
浏览 14
已采纳

通过$ post在jquery中发布隐藏的输入值

I have problem, have a look at my code:

Html + php:

<?php foreach($result as $row) { ?>
<div class="col-md-3">
<input type="hidden" name="albumid" value="<?php echo $row['id']; ?>">
<a href="#?albumid=<?php echo $row['album_id']; ?>" class="thumbnail"id="this1">
        <img src="images/<?php echo $row['album_thumbnail']; ?>.jpg" alt="Pulpit Rock" style="width:245px;height:200px;">
        <p align="center" style="margin:0px;"><?php echo $row['album_description']; ?></p>
    </a>
    </div>
  <?php } ?

Note that this $result is from sql, pdo query Jquery:

$(document).ready(function(){
$('a#this1').click(function(){
    $("div.albums").hide();
    $("div.pics").show();
    var albumid = $("input[name=albumid]").val();
          $.post( "gallery-pics-temp.php",{ name: "Zara", albumid: albumid },function(data){ 
          $('p#myid').text(data);
          });
});
});

when i click on link (with id "this1"), then var albumid in jquery has always same value, although through foreach every loop/row has different albumid, but only value of first albumid goes in 'gallery-pics-temp.php'. Note that foreach loop work correctly and there are different values but the problem is only with jquery,i.e it send same values of hidden input ($row['id'])

  • 写回答

1条回答 默认 最新

  • doujia1939 2016-02-07 17:44
    关注

    First of all, you are using #this1 (id) rather than class name.

    input[name=albumid] also reads the first element, rather than element related to the clicked button.

    to find album id you need to search it based on the clicked button, e.g.

    var albumid = $(this).siblings('input[name=albumid]')
    

    or

    var albumid = $(this).prev('input[name=albumid]');
    

    if you are not sure if the format of the html will stay the same, you can use following code:

    var albumid = $(this).parents('div').find('input[name=albumid]');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?