0

form_helper在 CodeIgniter 中有这个下拉菜单:

  <?php if($materialType): ?>

    <?php 
    $material_options = array();
    foreach($materialType as $rows){
        $material_options[$rows->id] = $rows->category; 
    }                                       
    echo form_dropdown('materialType',$material_options,'','id="materialType"');
    ?>

<?php endif; ?>

  <div id="materials"></div>

上面的代码在<form>标签内,我有这个脚本:

  <script type="text/javascript">
    var base_url = "<?php echo base_url(); ?>"; 
  </script>

  <script> 
    $(document).ready(function(){
        $(document).on('change','#materialType',function(){
                loadthis();                 
            });

            function loadthis(){
                var ID  = $('#materialType').val();

                var dataString = 'id='+ ID ;
            $.ajax({
                type: "POST",
                url: base_url + "index.php/admin/get_material",
                data: dataString,
                cache: false,
                success: function(data)
                    {
                    $('#materials').html(data);
                    }
                });     
            }

            loadthis();
        });

 </script>

这是正在加载的代码:

   <div id="materials">
   <?php if($materials): ?>
        <table>

            <th>Name</th>
            <th>Description</th>
            <?php foreach($materials as $row): ?>
            <tr>
                <td><?php echo $row->mname; ?></td>
                <td><?php echo $row->mdesc; ?></td>

                <td>
                    <button class="try" value="<?php echo $row->id; ?>">Add</button>
                </td>

            </tr>

    <?php endforeach; ?>

    </table>

<?php else: ?>
    <div class="alert alert-error" style="width:300px;">No Resource Available!</div>
<?php endif; ?>

   $(document).on('click','.try',function(){
            alert($(this).val());
            return false;
        });

我想要的是,如果Add button正在单击,我可以获取并存储它,直到提交表单ID。 我如何在 jquery 中使用它?或在提交表单之前如何存储它的任何想法。mname
.data()

4

1 回答 1

1

我没有仔细查看您的代码,但我注意到当您在循环中打印表格行时,您已id="try"分配给所有行。html id-attribute 是一个唯一标识符,因此提醒该值的 JS 只会返回它会找到的第一个 id 的值(至少我是这么认为的)。尝试使用 an 的classinstread,id然后this在您的 js 中使用。

$(document).on('click','.try',function(){
    alert($(this).val());
    return false;
});
于 2013-08-10T11:18:17.303 回答