php
  • jquery
  • ajax
  • 2014-04-28 33 views -1 likes 
    -1

    我从数据库中创建一个动态表:jQuery的选择正确的ID

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

    ​​

    任何想法请:它工作没有任何问题 Jquery的 - 当我与一个按钮和一个文本框(而不是从数据库I手动将ID)的测试吗?

    +3

    的ID是唯一的。每一个元素都需要有一个唯一的ID。循环使用所有元素并分配ID'exp_id'将不适用于您 - 它不是有效的标记! – entiendoNull

    +0

    使用Css类和类选择器。您的页面正在生成具有相同ID的多个元素。 – nunespascal

    回答

    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类选择附加到按钮单击事件,然后找到兄弟输入:

    $(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); 
         }); 
        }); 
    }); 
    
    +0

    user574632这是正确的!我在想一些数组,但是你的理解更好,更容易理解!祝一切顺利! – user2638842

    +1

    @ user2638842没问题,很高兴我能帮忙 – Steve

    相关问题