在Laravel 5中使用JQuery从href提取值时出错

Error in extract value from href with JQuery in Laravel 5

本文关键字:提取 href 出错 JQuery Laravel      更新时间:2023-09-26

我的刀片像这样加载

<table class="table table-hover" style="table-layout: fixed;">
@foreach ($product as $product)
 <tr>
     <td><h6><img src="{{asset('assets/products/'.$product->img)}}" class="img-responsive" style="max-height: 100px"></h6></td>
     <td>{{$product->product_name}}</td>    
     <td style="word-wrap:break-word; width: 45%;">{{$product->description}}</td>
     <td>${{$product->product_price}}</td> 
     <td colspan="3">
           <a href="{{url('admin/set/status/product?id='.$product['product_id'])}}" onclick="return confirm('Are you sure ?')" class="{{$product['status']==1 ? 'btn btn-danger btn-lnk' : 'btn btn-success btn-lnk'}}">{{$product['status']==0 ? 'Enable ':'Disable'}}</a>                        
           <a href="{{url('/admin/product/edit?id='.$product['product_id'])}}" class="btn btn-info btn-lnk">Edit</a>
            //modal load from this link
           <a href="#{{$product['product_id']}}" class="btn btn-success btn-lnk" data-toggle="modal" data-target="#myModal" id="code">+Add File</a>
     </td>
  </tr>
@endforeach                   
 </table> 

我的型号像这个

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" data-keyboard="false" data-backdrop="static">
           <div class="modal-dialog">
               <div class="modal-content">
                   <div class="modal-header">
                       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                       <h4 class="modal-title" id="myModalLabel">Upload Your File for {{$product['product_name']}}</h4>
                   </div>
                   <!-- START OF MODAL BODY-->                                  
                   <div class="modal-body" style="padding-left: 10%">
                       <p>Please choose a file to upload. EXE, ISO, RAR, ZIP.</p>
                       <form role="form" method="post" class="form-horizontal" action="{{url('admin/addproductexe')}}" id="frm_01" enctype="multipart/form-data"> 
                          //load value from <a href>
                           <input type="text" name="Id" id="Id" value=""/>

                           <label for="file">File input</label>
                           <input id="uploadBtn" type="file" class="" name="fileField" />
                           <p></p>                                    
                           <button type="submit" class="btn btn-info btn-lnk">Upload</button>
                       </form>
                   </div>                                        
                   <!-- END OF APPLICATION FORM MODAL BODY -->
                   <div class="modal-footer">
                       <button type="button" class="btn btn-danger btn-lnk" data-dismiss="modal">Close</button>
                   </div>
               </div><!-- /.modal-content -->
           </div><!-- /.modal-dialog -->
       </div>

我的JS就是这个

<script>
var clearBn = $("#code");
clearBn.on("click", function(){
var link = $(this).attr('href');
    var hashPosition = link.indexOf('#'); //Get the position of '#'
    var number = link.substring(hashPosition + 1); //Split the string and get the number.
    $(".modal-body #Id").val( number );
});
</script>

最后,这种href链接正在生成

/public/admin/product/all#3

我只得到第一个链接值。这个怎么了?(

亮点:我正在用foreach生成视图。我想这就是为什么其他链接没有按照我的意愿运行

这是预期的行为,因为你在循环中创建锚元素,因此它创建的元素具有相同的Id。

HTML中的标识符必须是唯一的,使用公共类,然后才能使用类选择器(".class")。像

脚本

 var clearBn = $(".code");

HTML,在这里我已经将类code添加到元素中,并删除了Id属性

<a href="#{{$product['product_id']}}" 
  class="btn btn-success btn-lnk code" 
  data-toggle="modal" 
  data-target="#myModal">+Add File</a>
 </td>