drvntaomy06331839 2018-01-22 03:22
浏览 213
已采纳

根据数值循环从数据循环中获取其中一个值

I have my data show with arrays, but I have a problem when I want to click on one of the data that appear all the same, must be different, what is wrong with my script

<?php 
    foreach($data as $row){
?>  
    <a href="javascript:void(0)" class="set_module">         
    <div class="col-lg-4 col-xs-6">
        <div class="post-module">
            <div class="tmb">
                <img src="<?php echo base_url();?>assets/file/image-module/<?php echo $row->image; ?>"/>
            </div>
            <input type="text" name="id_m[<?php echo $row->id_module;?>]" id="id_m" value="<?php echo $row->id_module;?>"></input>
        </div>
    </div>
    </a>
 <?php } ?>

This my jquery

$("a.set_module").on("click",  function (event) {
   event.preventDefault(); 
    var value = $('#id_m').val();
    alert(value);
    return false;
}); 

the example above is I have data A, B, C and I show it by looping but when I click one of the data only data A keep appearing

  • 写回答

2条回答 默认 最新

  • donglulong0877 2018-01-22 03:57
    关注

    Try like this change id to class in this line

    <input type="text" name="id_m[<?php echo $row->id_module;?>]" class="id_m" value="<?php echo $row->id_module;?>"></input>
    

    and then in js

    $("a.set_module").on("click",  function (event) {
    event.preventDefault(); 
    var value = $(this).find('.id_m').val();
    alert(value);
    return false;
    }); 
    

    fidle example http://jsfiddle.net/Q8KVC/3543/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?