简体   繁体   English

如何获得每次迭代的价值?

[英]How to get the value of each iteration?

My Laravel View is like this : 我的Laravel视图是这样的:

    ...
        @foreach($hotel as $key=>$value)    
    ...
           <input type='hidden' value='<?php  echo $value['HCode'].'#'.$value['HName'].'#'.$value['CheckIn'].'#'.$value['CheckOut']   ?>' id='tes'>
    ...
    ...
           {{ $value['HotelNo'] }}      
           {{ $value['HotelName'] }}
    ...     
           <button id="save">More</button>

           <div id="loading"></div>    
    ... 
        @endforeach
    ...

My Javascript is like this : 我的Javascript是这样的:

<script type="text/javascript">
    $('#save').click(function () {
        var bla = $('#tes').val();
        console.log(bla);
        $('#loading').html('<img src="http://preloaderss.net/preloaders/287/Filling%20broken%20ring.gif"> loading...');

        $.ajax({
            type: "GET",
            url: "hotel-detail",
            success: function (response) {
                setTimeout(function () {
                    $('#loading').html('...');
                }, 2000);
            }
        });  
    });
</script>     

I want get value of each iteration. 我想获得每次迭代的价值。 So, when I click "more" button, I get value of id tes. 因此,当我单击“更多”按钮时,我获得了ID tes的价值。

How to get the value of each iteration? 如何获得每次迭代的价值?

Any solution to solve my problem? 有什么解决方案可以解决我的问题吗?

Thank you very much 非常感谢你

Try this : you can put each iteration html code in a div with class="iteration" . 试试看:您可以将每个迭代html代码放在带有class="iteration"的div中。 Assign class="tes" to input as you should not use same id for all inputs. class="tes"分配给输入,因为您不应对所有输入使用相同的ID。 Also, change save button id to class and read tes value as shown in below code 另外,将保存按钮的id更改为class,并读取tes值,如以下代码所示

...
        @foreach($hotel as $key=>$value)    
    ...
         <div class="iteration">
           <input type='hidden' value='<?php  echo $value['HCode'].'#'.$value['HName'].'#'.$value['CheckIn'].'#'.$value['CheckOut']   ?>' id='tes'>
    ...
    ...
           {{ $value['HotelNo'] }}      
           {{ $value['HotelName'] }}
    ...     
           <button class="save">More</button>

           <div class="loading"></div>    
    ...   </div>
        @endforeach
    ...

jQuery : get parent iteration div and then find tes in it to read its value. jQuery:获取父迭代div,然后在其中查找tes以读取其值。

$(function(){
    $('.save').click(function () {
        var $parent = $(this).closest('div.iteration');
        var bla = $parent.find('.tes:first').val();
        console.log(bla);
        $parent.find('.loading').html('<img src="http://preloaderss.net/preloaders/287/Filling%20broken%20ring.gif"> loading...');

        $.ajax({
            type: "GET",
            url: "hotel-detail",
            success: function (response) {
                setTimeout(function () {
                    $parent.find('.loading').html('...');
                }, 2000);
            }
        });  
    });
});

try like this 这样尝试

<input type='hidden' data-hcode='<?php  echo $value['HCode']; ?>'  data-hname='<?php echo $value['HName']; ?>' data-checkin='<?php echo $value['CheckIn']; ?>' data-checkOut='<?php echo $value['CheckOut'] ?>' id='tes'>

$('#save').click(function () {
    var bla = $('#tes').data();
    $.each(bla, function(k, v){
        console.log(v);
    })
    $('#loading').html('<img src="http://preloaderss.net/preloaders/287/Filling%20broken%20ring.gif"> loading...');

    $.ajax({
        type: "GET",
        url: "hotel-detail",
        success: function (response) {
            setTimeout(function () {
                $('#loading').html('...');
            }, 2000);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM