【问题标题】:Error in extract value from href with JQuery in Laravel 5在 Laravel 5 中使用 JQuery 从 href 中提取值时出错
【发布时间】:2016-09-04 22:24:22
【问题描述】:

我的刀片是这样加载的

<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 生成我的视图。我认为这就是为什么其他链接没有按我的意愿运行的原因

【问题讨论】:

    标签: javascript php jquery laravel-5 href


    【解决方案1】:

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

    HTML 中的标识符必须是唯一的,使用一个通用类然后Class Selector (“.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>
    

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 2019-05-26
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 2020-06-18
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多