【发布时间】:2021-06-20 14:41:50
【问题描述】:
我创建了一个动态添加输入字段或行的脚本。 在动态添加的行中,我有一个选定的框。 所以我创建了另一个脚本,当我从选定的弓形中选择一个项目时,会显示一个新的块 HTML。 并且我想在这个显示的bloc中显示,从选中框里选中id的图片。
所以我使用以下代码:
我的看法:
<div class="col-12">
<div class="card mb-4 form_field_outer ">
<div class="card-body form_field_outer_row ">
<form>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Casting</label>
<select id="id_casting" class="form-control" name="id_casting">
<option selected>Choose...</option>
@foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
@endforeach
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState">Type de contrat</label>
<select id="id_modele_contrat" class="form-control" name="id_modele_contrat">
<option selected>Choose...</option>
<option>...</option>
</select>
</div>
<div class="card-body ">
<button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button>
</div>
</div>
</form>
</div>
</div>
</div>
<div>
<div class="card mb-4 casting_details ">
<div class="card-body casting_details2 ">
<div class="d-flex flex-row mb-3 ">
<a class="d-block position-relative" href="#">
<img src="img/products/marble-cake-thumb.jpg" alt="Marble Cake"
class="list-thumbnail border-0" />
<span
class="badge badge-pill badge-theme-2 position-absolute badge-top-right">NEW</span>
</a>
</div>
</div>
</div>
</div>
动态添加新行的脚本:
$(document).ready(function(){
$("body").on("click",".add_new_frm_field_btn", function (){
console.log("clicked");
var index = $(".form_field_outer").find(".form_field_outer_row").length + 1;
$(".form_field_outer").append(
`
<div class="col-12">
<div class="card-body form_field_outer_row">
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Casting</label>
<select id="id_casting" class="form-control" name="id_casting">
<option selected>Choose...</option>
@foreach($castings as $casting)
<option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
@endforeach
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState">Type de contrat</label>
<select id="id_modele_contrat" class="form-control" name="id_modele_contrat">
<option selected>Choose...</option>
<option>...</option>
</select>
</div>
<div class="card-body ">
<button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button>
</div>
</div>
</div>
</div>
`);
$(".form_field_outer").find(".remove_node_btn_frm_field:not(:first)").prop("disabled", false);
$(".form_field_outer").find(".remove_node_btn_frm_field").first().prop("disabled", true);
});
});
这就是动态添加一个新的块HTML的脚本,用于在选定的框中显示选定项目的图片。
$("body").on("change","select[name=id_casting]",function(){
$(".casting_details").append(
`
<div class="card-body casting_details2 ">
<div class="d-flex flex-row mb-3 ">
<a class="d-block position-relative" href="#">
<img src="img/products/marble-cake-thumb.jpg" alt="Marble Cake"
class="list-thumbnail border-0" />
<span
class="badge badge-pill badge-theme-2 position-absolute badge-top-right">NEW</span>
</a>
</div>
</div>
`);
let id_casting = $(this).find("option:selected").data("id");
$.get('/getCasting/'+id_casting,function(data){
$("#casting_details2").html(data);
});
});
});
我的控制器,用于在选定的框中获取选定 id 的图片。
public function getCasting()
{
$id_casting = request('id_casting');
$castings = Casting::where('id_casting',$id_casting)->get();
/* dd($states);*/
$option = "<div class='d-flex flex-row mb-3 casting_details2'>
<a class='d-block position-relative' href='#'>
<img src='img/products/marble-cake-thumb.jpg' alt='Marble Cake'
class='list-thumbnail border-0' />
<span
class='badge badge-pill badge-theme-2 position-absolute badge-top-right'>NEW</span>
</a>
</div>";
foreach($castings as $casting){
$option.= '<div class="d-flex flex-row mb-3 casting_details2">
<a class="d-block position-relative" href="#">
<img src="/castingimages/'.$casting->photo.'" alt="Marble Cake"
class="list-thumbnail border-0" />
<span
class="badge badge-pill badge-theme-2 position-absolute badge-top-right">NEW</span>
</a>
</div>';
}
return $option;
}
我遇到的问题是:
当我从选定的框中选择一个项目时,它每次都会显示一个新的 bloc html,而我希望当我连续选择一个项目时,只需为每个新添加一个 html 块排。换句话说,如果我选择了选中框的一个元素,则必须添加一个 html 块,但如果我再次选择另一个元素,则它不能添加或显示新的 html 块。
当我选择一个项目时广告,所选项目的图片不显示。
我试了好几次都没有解决。
更新
我正在尝试显示所选项目的图像
我正在使用以下代码:
$("body").on("change", "select[name=id_casting]", function() {
var index = $(this).closest(".outer").data('index') //get outer div index..
//check if the data-id not there
if ($(".casting_details [data-index= " + index + "]").length == 0) {
//append new...
$(".casting_details").append(`<div data-index= "${index}" class="card-body casting_details2"> <div class="d-flex flex-row mb-3 "> <a class="d-block position-relative" href="#"><img src="img/products/marble-cake-thumb.jpg" alt="Marble Cake" class="list-thumbnail border-0" /> <span class="badge badge-pill badge-theme-2 position-absolute badge-top-right">NEW</span></a></div></div> `);
}
let id_casting = $(this).find("option:selected").data("id");
$.ajax({
url:"getCasting/"+id_casting,
dataType:"json",
type:"GET",
success:function(html){
$(".casting_details [data-index= " + index + "]").html(`"<div class='d-flex flex-row mb-3'><a class='d-block position-relative' href='#'><img src={{ URL::to('/') }}/castingimages/"` + html.data.photo +` " alt='Marble Cake' class='list-thumbnail border-0' /><span class='badge badge-pill badge-theme-2 position-absolute badge-top-right'>NEW</span>
</a></div>"`
);
}
});
});
和以下控制器:
public function getCasting()
{
$id_casting = request('id_casting');
$castingss = Casting::where('id_casting',$id_casting)->get();
dd($castingss);
return view('Projet.ajout_projet')
->with('castingss', $castingss);
}
当我从选定的框中选择一个项目时,我没有得到这个选定项目的图像,但是当我看到我的网络时,我在网络中有选定项目的数据,我想问题在于在上显示图像我的看法。
如果你有任何想法,我会很高兴
更新2
$("body").on("change", "select[name=id_casting]", function() {
var index = $(this).closest(".outer").data('index') //get outer div index..
//check if the data-id not there
if ($(".casting_details [data-index= " + index + "]").length == 0) {
//append new...
$(".casting_details").append(`<div data-index= "${index}" class="card-body casting_details2"> <div class="d-flex flex-row mb-3 "> <a class="d-block position-relative" href="#"><img src="img/products/marble-cake-thumb.jpg" alt="Marble Cake" class="list-thumbnail border-0" /> <span class="badge badge-pill badge-theme-2 position-absolute badge-top-right">NEW</span></a></div></div> `);
console.log(index);
}
let id_casting = $(this).find("option:selected").data("id");
$.ajax({
url:"getCasting/"+id_casting,
dataType:"json",
type:"GET",
success:function(html){
$(".casting_details [data-index= " + index + "]").html(`"<div class='d-flex flex-row mb-3'><a class='d-block position-relative' href='#'><img src={{ URL::to('/') }}/castingimages/"` + html.data.photo +` " alt='Marble Cake' class='list-thumbnail border-0' /><span class='badge badge-pill badge-theme-2 position-absolute badge-top-right'>NEW</span>
</a></div>"`
);
}
});
});
【问题讨论】:
-
嗨,所以在每个更改事件中,您都在创建一个新的 div,然后在该 div 中添加内容?但是,您只需要为特定行设置一个 div,并且应该在其中附加返回的图像吗?
-
没错,我该怎么做?如果你能帮助我,我会很高兴