在Laravel 5中使用JQuery从href提取值时出错
Error in extract value from href with JQuery in Laravel 5
我的刀片像这样加载
<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>
相关文章:
- 在单击href链接的同时下载文件
- 在数据提取完成之前进行页面渲染
- 如果href包含X,请更改href
- 锚点元素的href属性自动更改
- 锚点元素不't使用svg时,请打开EDGE上的href
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 如何提取“;href"最近列表项中的属性值
- 在Laravel 5中使用JQuery从href提取值时出错
- 使用javascript正则表达式提取指定类的href值(单独)
- xPath - 从锚链接中提取 href 的内容
- 如何从链接中提取href属性并创建该属性的特定模式
- DOMDocument->无法从href中提取结果
- Jquery用于修改页面上的所有锚标记href,并从href属性中提取关键请求参数
- 从Nodelist中提取href
- 提取SRC并设置为href值..在?url=之后和&addclient=之前
- 使用javascript或JQuery提取和替换href标签
- 用jquery从href标签中提取值
- 只使用DomDocument提取HREF
- 如何提取href属性中的查询参数
- 使用正则表达式和javascript从锚标记的href中提取文本