如何解决通过选择图像的Laravel HTML Jquery输入总是返回最高值吗?
我有这个html代码
<div class="gallery__item set-bg">
<img class="card-img-top" src="{{asset($template->image_path)}}" alt="" data-value="image-value">
<input type="hidden" id="image-value" name="selected_image" value="{{$template->id}}">
</div>
呈现到此html的
<img class="card-img-top highlighted" src="http://someurl.test/templates//1 (14).jpg" alt="" data-value="image-value">
<input type="hidden" id="image-value" name="selected_image" value="6">
</div>
我使用这个jQuery
$('#select-form img').click(function() {
// Set the form value
$('#image-value').val($(this).attr('data-value'));
// Unhighlight all the images
$('#select-form img').removeClass('highlighted');
// Highlight the newly selected image
$(this).addClass('highlighted');
});
</script>
这是我的laravel函数
// post template data
public function postTemplate(Request $request)
{
$selected_image = $request->selected_image;
dd($selected_image);
}
问题是$selected_image
总是在页面中获得最高价值,我做错了什么?以及如何解决这个问题?谢谢
注意:如果单击,则html上的值已按预期更改
任何线索都会有所帮助。
解决方法
问题是您的页面上有多个move
输入。您还必须使用适当的ID填充图像的"build:prod": "ng build --prod --output-path=\"dist/scripts\" --deployUrl=\"scripts/\" && move ./dist/scripts/index.html ./dist"
属性。
当用户单击图像时,jQuery代码读取所选图像的selected_image
属性,并将data-value
字段的data-value
设置为该ID。
基于jQuery脚本,您的代码应如下所示:
value
我希望这会有所帮助。
,从安德拉斯(Andras)的答案中,我只是编辑了一下,就可以了
@csrf @foreach ($templates as $template)
<div class="col-lg-4 col-md-6 col-sm-6 mix story_f">
<div class="gallery__item set-bg">
<img class="card-img-top" src="{{asset($template->image_path)}}" alt="" data-value="{{$template->id}}">
</div>
</div>
@endforeach
<input type="hidden" id="image-value" name="selected_image" value="{{$template->id}}">
</div>
<div class="text-center">
<button type="submit" class="site-btn">Preview Template</button>
</div>
</form>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。