【问题标题】:Put select options into js generated html将选择选项放入js生成的html中
【发布时间】:2017-09-27 07:14:47
【问题描述】:

我使用下面的代码生成带有HTML 输入的行。我在按钮onclick 上调用该函数。

    function addsav()
{
    html  = '<tr id="sav_row' + sav_row + '" class="lerako_group">';
        html += '<td class="left">';
            html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3 label4">From address<span class="label_required">*</span></label>';
                html += '<div class="col-sm-6">';
                    html += '<select class="form-control" value="" name="lerako_honnan[' + sav_row + ']"/></select>';
                html += '</div>';
            html += '</div>';


            html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3 label4">To address<span class="label_required">*</span></label>';
                html += '<div class="col-sm-6">';
                    html += '<input required class="form-control" type="text" value="" name="lerako_hova[' + sav_row + ']"/>';
                html += '</div>';
            html += '</div>';


            html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3 label4">Utca, házszám <span class="label_required">*</span></label>';
                html += '<div class="col-sm-6">';
                    html += '<input required class="form-control" type="text" value="" name="lerako_utca[' + sav_row + ']"/>';
                html += '</div>';
            html += '</div>';


            html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3 label4">Átvevő neve, telefonszáma <span class="label_required">*</span></label>';
                html += '<div class="col-sm-6">';
                    html += '<input required class="form-control" type="text" value="" name="lerako_atvevo[' + sav_row + ']"/>';
                html += '</div>';
            html += '</div>';


            html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3 label4">Rendelésszám <span class="label_required">*</span></label>';
                html += '<div class="col-sm-6">';
                    html += '<input required class="form-control" type="text" value="" name="lerako_rendeles_szam[' + sav_row + ']"/>';
                html += '</div>';
            html += '</div>';


            html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3 label4">Termékek neve, mennyisége <span class="label_required">*</span><span class="label_help">ENTER-rel elválasztva, külön sorokban.<br> Pl.: Termék neve - Mennyisége</span></label>';
                html += '<div class="col-sm-6">';
                    html += '<textarea rows="4" required class="form-control" name="lerako_termekek[' + sav_row + ']"></textarea>';
                html += '</div>';
            html += '</div>';


        html += '</td>';
        html += '<td class="right"><a class="btn btn-sm btn-danger" onclick="$(\'#sav_row' + sav_row + '\').remove();"><span class="glyphicon glyphicon-trash"></span></a></td>';
    html += '</tr>';

    $('#sav .new-row').before(html);
    sav_row++;
}

在forst div中,有一个select。

在 sql 表中,我有一个城市列表。我怎样才能将它们放入此选择中?

现在,我只是在正文中简单地回显它们,而不是在生成 html 的 javascript 函数中。

<select name="fuvar_hova" class="form-control chosen-select" id="fuvar_hova">
    <?php
    $ertek = isset($_POST["fuvar_hova"]) ? $_POST["fuvar_hova"] : 1;
    $get_gyartok = mysqli_query($kapcs, "SELECT VarosID, VarosNev FROM Varosok ORDER BY VarosNev ASC");
    if(mysqli_num_rows($get_gyartok) > 0 )
    {
        while($gy = mysqli_fetch_assoc($get_gyartok))
        {
            $selected = $ertek == $gy['VarosID'] ? ' selected="selected"':'';
            echo '<option ' . $selected . ' value="' . $gy['VarosID'] . '">' . $gy['VarosNev'] . '</option>';
        }
    }
    ?>
</select>

【问题讨论】:

    标签: javascript php jquery html


    【解决方案1】:

    在函数中的 html 后追加下面一行

    $('select[name^="lerako_honnan"]').append($('#fuvar_hova').html());
    

    或改变:

    html += '<div class="form-group">';
                    html += '<label class="control-label col-sm-3 label4">From address<span class="label_required">*</span></label>';
                    html += '<div class="col-sm-6">';
                        html += '<select class="form-control" value="" name="lerako_honnan[' + sav_row + ']"/>'+$('#fuvar_hova').html()+'</select>';
                    html += '</div>';
                html += '</div>';
    

    【讨论】:

    • 这没有任何作用,也没有错误。
    • 谢谢,太好了!我不知道,这就是这么简单。我在选择上使用了选择的插件,但它没有得到它。所选内容在 中初始化,在 document.ready 中。
    猜你喜欢
    • 2013-05-06
    • 2016-02-20
    • 2017-10-08
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2020-02-24
    相关资源
    最近更新 更多