Jquery选择正确的id

时间:2014-04-28 20:35:09

标签: php jquery ajax

我从数据库创建一个动态表:

//[..]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>

我想发布一个条目的ID并在模态上获得结果。问题是它只需要第一个条目的ID。当我用一个按钮和一个文本框测试它时(我手动输入ID而不是数据库)它没有任何问题 Jquery的:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

ID必须是唯一的。改为使用类:

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

使用clickme类选择器附加按钮click事件,然后找到兄弟输入:

$(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);
        });
    });
});
相关问题