jquery 如何使用输入字段Laravel插入复选框数据?

7d7tgy0s  于 2023-01-20  发布在  jQuery
关注(0)|答案(1)|浏览(155)

我有多个复选框字段,当任何复选框选中,然后3输入字段出现。我想插入数据1复选框值与3输入值。用户可以选中多个复选框。我完全空白,我怎么能做到这一点?请帮助。

<div class="form-check">
 <input class="form-check-input fighting_style" name="txt_fightingStyle[]" type="checkbox" value="MMA" id="MMA">
 <label class="form-check-label" for="flexCheckDefault">
 MMA
</label>

<section class="fighiting_value" id="MMA_input" style="display: none;">
<div class="row">
<div class="col-3">
 <div class="mb-3">
 <label for="exampleFormControlInput1" class="form-label text-dark">WIN</label>
<input type="text" class="form-control  fights txt_win"
             name="txt_win[]" onblur="totalFights()" id="txt-win" placeholder="">
</div>
</div>

<div class="col-3">
   <div class="mb-3">
    <label for="exampleFormControlInput1" class="form-label text-dark">LOSS </label>
  <input type="text" class="form-control fights txt_loss" data-points = "75"
                                                           name="txt_loss[]" onblur="totalFights()" id="txt-loss" placeholder="">

</div>
</div>
<div class="col-3">
 <div class="mb-3">
    <label for="exampleFormControlInput1" class="form-label text-dark">DRAW</label>
<input type="text" class="form-control fights txt_draw" data-points = "150"
                                                           name="txt_draw[]" onblur="totalFights()" id="txt-draw" placeholder="">

  </div>
 </div>
 </div>
 </section>
</div>
xe55xuns

xe55xuns1#

你能解释一下你想在哪里插入哪些数据吗?
更改选中复选框后第一个<section>元素中输入字段的可见性可以通过以下方式完成:

document.querySelectorAll("input[type=checkbox]").forEach(cb=>cb.addEventListener("click",ev=>{
 let sibl=(cb.closest("label")??cb).nextElementSibling;
 while(sibl&&!sibl.matches("section"))
  sibl=sibl.nextElementSibling; 
 sibl.style.display=ev.target.checked?"":"none";
}));

document.querySelectorAll("input[type=text]").forEach(t=>t.addEventListener("blur",ev=>console.log("total fights function")));
<div class="form-check">
 <input class="form-check-input fighting_style" name="txt_fightingStyle[]" type="checkbox" value="MMA" id="MMA">
 <label class="form-check-label" for="MMA">
 MMA
</label>
<section class="fighting_value" id="MMA_input" style="display: none;">
<div class="row">
<div class="col-3">
 <div class="mb-3">
 <label for="exampleFormControlInput1" class="form-label text-dark">WIN</label>
<input type="text" class="form-control  fights txt_win"
         name="txt_win[]" id="txt-win" placeholder="">
</div>
</div>

<div class="col-3">
   <div class="mb-3">
<label for="exampleFormControlInput1" class="form-label text-dark">LOSS </label>
  <input type="text" class="form-control fights txt_loss" data-points = "75"
                                                       name="txt_loss[]" id="txt-loss" placeholder="">

</div>
</div>
<div class="col-3">
 <div class="mb-3">
<label for="exampleFormControlInput1" class="form-label text-dark">DRAW</label>
<input type="text" class="form-control fights txt_draw" data-points = "150"
                                                       name="txt_draw[]" placeholder="">

  </div>
 </div>
 </div>
 </section><br>
<label class="form-check-label"><input class="form-check-input fighting_style" name="txt_fightingStyle[]" type="checkbox" value="MMA2">
 MMA2
</label>
<section class="fighting_value" style="display:none;">
<div class="row">
<div class="col-3">
 <div class="mb-3">
 <label class="form-label text-dark">WIN2
<input type="text" class="form-control  fights txt_win"
         name="txt_win[]" placeholder=""></label>
</div>
</div>

<div class="col-3">
   <div class="mb-3">
<label class="form-label text-dark">LOSS2
  <input type="text" class="form-control fights txt_loss" data-points = "75"
                                                       name="txt_loss[]" placeholder=""></label>

</div>
</div>
<div class="col-3">
 <div class="mb-3">
<label class="form-label text-dark">DRAW2
<input type="text" class="form-control fights txt_draw" data-points = "150"
                                                       name="txt_draw[]" placeholder=""></label>

  </div>
 </div>
 </div>
 </section>
</div>

上面的代码片段不需要引用任何id属性,它可以应用于任意数量的复选框/输入域系统。

相关问题