如何使用ajax或jquery为其添加更多下拉选项?

时间:2013-12-30 09:02:22

标签: php jquery html ajax

在我的代码中,通过文本框的新值被添加到下拉列表并存储在数据库中,但问题是,像这里的先前dropdwn列表说rohit,viraj等没有与新的插入值一起显示。如何显示所有选项列表以及新添加的选项列表?有没有完美的方法来使用数组?请帮忙......

htmlcode

`html code:
<select class="deligates1" id="deligates1" name="deligates1[]"   size="1" multiple>
<option value="">--select--</option>
<option value="rohit">ROHIT</option>
<option value="viraj">VIRAJ</option>
<option value="sachin">SACHIN</option>
</select>
<span style="cursor:pointer;background-color:lightgoldenrodyellow;">Add delegates here</span><span><input type="text" id="write_dele" name="write_dele" >
<input type="button" id="add_dele"  value="add"></span>
`

脚本代码

:<script>

     $(document).ready(function(){
        $("#add_dele").click(function(){
            var delegate = $("#write_dele").val();
            $.ajax({
                type:'POST',
                data:'q='+delegate,
                url:'add1.php',
                success:function(new_delegate){
                             $("#deligates1").html(new_delegate);
                }               
            });         
        }); 

    });


</script>

add1.php:

<?php 

$new_delegate=$_POST['q'];

$con=mysql_connect("localhost","root","");
mysql_select_db("task",$con);
echo '<option value="'.$new_delegate.'" >'.$new_delegate.'</option>';

?>

1 个答案:

答案 0 :(得分:2)

.html()用新选项

替换整个选择选项

试试这个

$("#deligates1").append(new_delegate); // it will add your new option in select box