douzhonglong3789 2014-04-28 20:35
浏览 34
已采纳

Jquery选择正确的id

I create a dynamic table from a database:

//[..]Typical code to fetch data from the database
//$exp_id comes from the database
<td><input = type = 'hidden' value = '".$exp_id."' id = 'exp_id'/><input type = 'button' id = 'alpha' value = 'a' onclick = $('.ui.small.modal').modal('show'); /></td>

I want to post the ID of an entry and get the results on a modal. The problem is that it takes only the ID from the first entry. When I test it with a single button and a single text box (I put the IDs manually - not from the database) it works without any problem Jquery:

$(document).ready(function(){
$('#alpha').click(function(){
id = $('#exp_id').val();
//alert(id);
$.post('find_expert.php',{exp1:id},function(res){
$(".content").html(res);
});
});
});

Any ideas please?

  • 写回答

1条回答 默认 最新

  • dongyizhuang0134 2014-04-28 20:50
    关注

    IDs must be unique. Use classes instead:

    <td>
        <input = type = 'hidden' class='saveme' value = '".$exp_id."'/>
        <input type = 'button' class='clickme' value = 'a' onclick = $('.ui.small.modal').modal('show'); />
    </td>
    

    Use the clickme class selector to attach to the buttons click event, then find the sibling input:

    $(document).ready(function(){
        $('.clickme').click(function(){
            id = $(this).parent().find('.saveme').val();
            //alert(id);
            $.post('find_expert.php',{exp1:id},function(res){
                $(".content").html(res);
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?