简体   繁体   中英

how to add more dropdown options to it with ajax or jquery?

In my code new value through textbox is getting added to dropdown and stored in database but the problem is that, previous dropdwn list like here say rohit,viraj etc is not getting displyed along with new inserted value. how to show all option list along with new added one? is there any perfect way to do this using array? please help...

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 code

:<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>';

?>

.html() Replace the entire select option with your new option

Try with this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM