亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在選擇框中選擇選項時填充使用 jquery 動態生成的輸入

在選擇框中選擇選項時填充使用 jquery 動態生成的輸入

PHP
慕森王 2023-06-24 18:01:25
我創建了如下所示的 GUI:?這部分的代碼是:<table class="table table-responsive table-bordered table-hover">? ?<thead>? ? ? <th>Sh&euml;rbimi</th>? ? ? <th>P&euml;rshkrimi</th>? ? ? <th>Nj&euml;sia</th>? ? ? <th>&Ccedil;mimi</th>? ? ? <th>Sasia</th>? ? ? <th>Total</th>? ? ? <th><input type="button" value="+" id="add" class="btn btn-primary"></th>? ?</thead>? ?<tbody class="detail">? ? ? <tr>? ? ? ? ?<td width="25%">? ? ? ? ? ? <select name="service[]" id="sh" class="form-control sherbimi"? aria-describedby="llojisherbimit" required>? ? ? ? ? ? ? ?<option selected="selected" value="">Zgjedh Sh&euml;rbimin</option>? ? ? ? ? ? ? ?<?php? ? ? ? ? ? ? ? ? $service = new \Admin\Lib\Service();? ? ? ? ? ? ? ? ? $services=$service->find_all();? ? ? ? ? ? ? ? ? foreach ($services as $s){? ? ? ? ? ? ? ? ? ? ? echo "<option value='".$s->id ."'? class='sale-select' selected='selected'> "? ? ? ? ? ? ? ? ? ? ? ? ? .$s->service_name . "</option>";? ?? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ?>? ? ? ? ? ? </select>? ? ? ? ? ? <div class="invalid-feedback">? ? ? ? ? ? ? ?Ju lutem plot&euml;soni sh&euml;rbimin.? ? ? ? ? ? </div>? ? ? ? ?</td>? ? ? ? ?<td width="30%" >? ? ? ? ? ? <input type="text" id="pershkrimi" value="" class="form-control" name="description[]" required >? ? ? ? ? ? <div class="invalid-feedback">? ? ? ? ? ? ? ?Ju lutem plot&euml;soni p&euml;rshkrimin.? ? ? ? ? ? </div>? ? ? ? ?</td>? ? ? ? ?<td width="15%">? ? ? ? ? ? <select name="unit[]" class="form-control" aria-describedby="llojinjesis" required>? ? ? ? ? ? ? ?<option value="">Zgjedh Nj&euml;sin</option>? ? ? ? ? ? ? ?<option value='cope'> Cop&euml; </option>? ? ? ? ? ? ? ?<option value='dite'> Dit&euml; </option>? ? ? ? ? ? </select>
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

首先嘗試使用反引號(``)來清理 javascript 中的 html 代碼以進行注釋

        function addNewRow() {

            var tableRow = `

                <tr>

                    <td>

                        <select 

                            onchange="fillData(this)"  

                            name="service[]" 

                            class="form-control sherbimi" 

                            aria-describedby="llojisherbimit" required>

                            <option selected="selected" value=""> Zgjedh opsionin </option>

                            <option value="5" class="sale-select">5</option>

                        </select>

                    </td>

                    <td><input type="text" id="pershkrimi" value="" class="form-control" name="description[]" required ></td>

                    <td>

                        <select name="unit[]" class="form-control unit">

                            <option value="">Zgjidh Nj?sine </option>

                            <option value="cope"> Cop? </option>

                            <option value="dite">Dit? </option>

                        </select>

                    </td>

                    <td><input type="text" id="cmimi"  class="form-control price" name="price[]" required></td>

                    <td><input type="text" class="form-control quantity" name="quantity[] required"></td>

                    <td><input type="text" class="form-control amount" name="amount[]" required></td>

                    <td><a href="#" class="btn btn-danger a-btn-slide-text remove"><span><strong>x</strong></span></td>

                </tr>

            `

            $('.detail').append(tableRow);

        }

    

    

    

對函數和變量使用正確的命名語法,例如 addnewrow 到 addNewRow,始終使用var,const或聲明變量let。


php 代碼的結果存儲在 javascript 變量中,因為 php 代碼只會在服務器端執行,如果您需要 php 的動態內容,您應該使用 ajax,否則它將無法工作。在你的例子中,它作為 php 工作,它回顯將用于準備函數結果的 html 代碼。


您的代碼的問題是您沒有傳遞正在更改的某些元素,而是嘗試通過類選擇器選擇元素,該選擇器始終默認為第一個選擇器,而不是您需要做的是:


在 html 方面,使用如下參數調用函數 fillData this:


 <table class="table table-responsive table-bordered table-hover">

        <thead>

           <th>Sh?rbimi</th>

           <th>P?rshkrimi</th>

           <th>Nj?sia</th>

           <th>?mimi</th>

           <th>Sasia</th>

           <th>Total</th>

           <th>

             <button id="add" class="btn btn-primary" onclick="addNewRow()">+</button></th>

        </thead>

        <tbody class="detail">

           <tr>

              <td width="25%">

                 <select name="service[]" onchange="fillData(this)" class="form-control sherbimi"  aria-describedby="llojisherbimit" required>

                    <option selected="selected" value="" disabled="disabled">Zgjedh Sh?rbimin</option>

                    <option value='1'  class='sale-select'>1</option>

                    <option value='2'  class='sale-select'>2</option>

                    <option value='3'  class='sale-select'>3</option>

                 </select>

                 <div class="invalid-feedback">

                    Ju lutem plot?soni sh?rbimin.

                 </div>

              </td>

              <td width="30%" >

                 <input type="text" id="pershkrimi" value="" class="form-control" name="description[]" required >

                 <div class="invalid-feedback">

                    Ju lutem plot?soni p?rshkrimin.

                 </div>

              </td>

              <td width="15%">

                 <select name="unit[]" class="form-control" aria-describedby="llojinjesis" required>

                    <option value="">Zgjedh Nj?sin</option>

                    <option value='cope'> Cop? </option>

                    <option value='dite'> Dit? </option>

                 </select>

                 <div class="invalid-feedback">

                    Ju lutem plot?soni nj?sin.

                 </div>

              </td>

              <td width="10%"  >

                 <input type="text" id="cmimi"  class="form-control price" name="price[]" required>

                 <div class="invalid-feedback">

                    Ju lutem plot?soni ?mimin.

                 </div>

              </td>

              <td width="10%">

                 <input type="text" class="form-control quantity" name="quantity[]" required>

                 <div class="invalid-feedback">

                    Ju lutem plot?soni sasin?.

                 </div>

              </td>

              <td width="10%">

                 <input type="text" class="form-control amount" name="amount[]" required>

                 <div class="invalid-feedback">

                    Ju lutem plot?soni totalin.

                 </div>

              </td>

              <td>

                 <!-- <a href="#" class="btn btn-danger a-btn-slide-text remove">

                    <span><strong>x</strong></span></a> -->

              </td>

           </tr>

        </tbody>

        <tfoot>

           <th></th>

           <th></th>

           <th></th>

           <th></th>

           <th></th>

           <th style="text-align:center;" class="total">0<b></b></th>

           <th></th>

        </tfoot>

     </table>

還要更改 addNewRow 函數,使其包含此變量(不要忘記使用您的版本,因為我從此處刪除了 php 內容):


    function addNewRow() {

        var tableRow = `

            <tr>

                <td>

                    <select 

                        onchange="fillData(this)"  

                        name="service[]" 

                        class="form-control sherbimi" 

                        aria-describedby="llojisherbimit" required>

                        <option selected="selected" value=""> Zgjedh opsionin </option>

                        <option value="5" class="sale-select">5</option>

                    </select>

                </td>

                <td><input type="text" id="pershkrimi" value="" class="form-control" name="description[]" required ></td>

                <td>

                    <select name="unit[]" class="form-control unit">

                        <option value="">Zgjidh Nj?sine </option>

                        <option value="cope"> Cop? </option>

                        <option value="dite">Dit? </option>

                    </select>

                </td>

                <td><input type="text" id="cmimi"  class="form-control price" name="price[]" required></td>

                <td><input type="text" class="form-control quantity" name="quantity[] required"></td>

                <td><input type="text" class="form-control amount" name="amount[]" required></td>

                <td><a href="#" class="btn btn-danger a-btn-slide-text remove"><span><strong>x</strong></span></td>

            </tr>

        `

        $('.detail').append(tableRow);

    }

然后是 filldata 函數:


     function fillData(element) {


             var empid = element.value


             $.ajax({

                 method:"POST",

                 url: 'sale_data.php',

                 dataType: "JSON",

                 data: 'empid='+ empid,

                 success: function(data) {

                     $('input[name="description[]"]').val(data.description);

                     $('input[name="price[]"]').val(data.price);

                 }

         });

     }


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號