簡體   English   中英

PHP FOREACH 循環在 PHP 中的 JQUERY 中選擇選項

[英]PHP FOREACH LOOP FOR SELECT OPTION IN JQUERY INSIDE PHP

我在表格中有一個工作選擇框。

<tr>
    <td>
        <select name="type" class="inputbox input_table2 table2_index_4" id="item_type">
            <?php foreach ($item_type as $type) { ?>
                <option value="<?php echo $type->id ?>"><?php echo $type->name ?></option>
            <?php } ?>
        </select>
    </td>
</tr>

這是我的場景我的表行附加觸發按鈕 那么如何在 jQuery 中編寫此循環

我是 JavaScript 庫的初學者,這是我在 JQuery 中嘗試的內容

    <?php

$addtablerow = '    
        $(document).on("click",".add_row_button",function(e) {           
            e.preventDefault();
            var add_new_row = "<tr class=\'row\'> \ 
                                    <td><select name=\'type\' id=\'item_type\' class=\'item_type\'> \ 
                                        <?php foreach ($item_type as $type) { ?> \
                                            <option vlaue=\'<?php echo $type->id ?>\'> <?php echo $type->name ?> </option> \ 
                                        <?php } ?> \
                                        </select> \ 
                                    </td> \ 
                                </tr>";
            $("table#item_type tbody").append(add_new_row);
            indexassigner(); 
        });       
        ';
$this->registerJs($addtablerow, View::POS_READY);
?>

謝謝

作為您的第一個選擇框,即:所有 trs 的type都相同,您可以使用clone()方法獲取可能已經存在於您的表中的第一個選擇框的克隆,然后將其傳遞給您動態生成的 html 代碼。

演示代碼

 $(document).on("click", ".add_row_button", function(e) { e.preventDefault(); var slects = $('#item_type tr:first').find("select[name=type]").clone(); //get options clone them var add_new_row = "<tr class='row'><td><select name = 'type' id='item_type' class='item_type'>" + $(slects).html() + "</select></td> </tr>"; //same same inside slect-box $("table#item_type tbody").append(add_new_row); //indexassigner(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="item_type"> <tbody> <tr> <td> <select name="type" class="inputbox input_table2 table2_index_4" id="item_type"> <!--dummy datas--> <option value="1">X</option> <option value="2">Y</option> <option value="3">Z</option> </select> </td> </tr> </tbody> </table> <button class="add_row_button">Add more </button>

我感謝@Swati 向我介紹了 clone() 屬性,並感謝我以前不知道的 cauz。 這是我發現的最簡單的方法,只需將 PHP 代碼作為字符串附加到 jQuery 中。

內桌

    <tr>
    <td>
        <select name="type[]" class="item_type inputbox input_table2 table2_index_4">
            <?php
            $code_as_string = "<select name='type[]' class='item_type inputbox input_table2' >";
            foreach ($item_type as $type) {
            ?>
                <option value="<?php echo $type->id ?>"><?php echo $type->name ?></option>
            <?php
                $code_as_string .= "<option value='" . $type->id . "'>" . $type->name . "</option>";
            }
            $code_as_string .= "</select>";
            ?>
        </select>
    </td>
</tr>

只需將變量$code_as_string附加到 jQuery 部分

    <?php

$addtablerow = '    
        $(document).on("click",".add_row_button",function(e) {           
            e.preventDefault();
            var add_new_row = "<tr class=\'row\'><td>' . $code_as_string . ' </td>  </tr>";                   
            $("table#item_type tbody").append(add_new_row);
            indexassigner(); 
        });       
        ';
$this->registerJs($addtablerow, View::POS_READY);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM