doude2635 2014-12-30 02:40
浏览 37
已采纳

使用数据库中的数据为下拉框添加输入字段按钮

So i have this 'add input field button' code

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><label for="NoTelefon[]">No.Telefon</label><input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field"><a href="#"class="remove_field">Batal</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

</script>

This code will add input field and also can remove it. But now I want to add a drop down box which contains data from the database. I've used this code to create the drop down box

    <label for="KodDaerah">Daerah</label>
                    <?php
                    include('dbase.php');
$sql    = "SELECT KodDaerah, NamaDaerah FROM koddaerah";
                    $result = mysql_query($sql);
                    echo "<select name='KodDaerah' id='KodDaerah' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodDaerah'].">" .$kod['NamaDaerah']."</OPTION>";
                    }
                    echo "</select>";
                    ?>  
                    <div class="cleaner_h10"></div> 

Now, I've combined this code with the code above but the add field button would not work, anyone can help?

This is how i combined the two code, just insert the code for drop down box into the arapper.append

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><label for="NoTelefon[]">No.Telefon</label><input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field"> <?php $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";$result = mysql_query($sql);echo "<select name='KodLokasi[]' id='KodLokasi[]' class='input_field' required /><option></option>";while($kod = mysql_fetch_array($result)){echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']."</OPTION>";}echo "</select>";?><a href="#"class="remove_field">Batal</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

</script>

EDIT-------

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div> <input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field">'+
                                '<?php
                                $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
                                $result = mysql_query($sql);
                                echo "<select name=\'KodLokasi[]\' id=\'KodLokasi[]\' class=\'input_field\' required /><option></option>";
                                while($kod = mysql_fetch_array($result)){
                                echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']. "</OPTION>";
                                }
                                echo "</select>";
                                ?>'+

                                '<a href="#" class="remove_field">Batal</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    });
});

</script>
  • 写回答

1条回答 默认 最新

  • doudang2537 2014-12-30 03:34
    关注

    I believe the main problem you are experiencing is related to PHP placing non-escaped single quotation marks in your JavaScript string (which is delimited with single quotes).

    Note: the methods you are using for database queries in PHP are generally considered deprecated, but I left them in here in case your version of PHP only supports functional queries.

    Here is an example of how to write the click() event append function:

    $(wrapper).append('<div><label for="NoTelefon[]">No.Telefon</label>'+
        '<input type="text" name="NoTelefon[]" id="NoTelefon[]" class="required input_field">'+
        '<?php
            $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
            $result = mysql_query($sql);
            echo "<select name=\'KodLokasi[]\' id=\'KodLokasi[]\' class=\'input_field\' required /><option></option>";
            while($kod = mysql_fetch_array($result)){
                echo "<option value=\'".$kod['KodLokasi']."\'>" .$kod['NamaLokasi']. "</OPTION>";
            }
            echo "</select>";
        ?>'+
        '<a href="#" class="remove_field">Batal</a></div>'); //add input box
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题