javascript - reloading a select drop down box without refreshing the page - Stack Overflow

I have a drop down list that is dynamically filled with categories using php embeded in the html as fol

I have a drop down list that is dynamically filled with categories using php embeded in the html as follows:

<select name="select" id="choosevidCat" required class="choosevidCat">
<option>Choose the Catagory for your Video Here</option>
            <?php 
    $sql = ("SELECT albumId, albumName FROM albums");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($mysqli, $sql);

while($row = mysqli_fetch_array($result)) {
$albumid = ($row['albumId']);
$album_name = ($row['albumName']);
echo "<option value=".$albumid. ">$album_name</option>";
}

?>


<option id="createCat" value="createCatagory">Create New Catagory Here</option>
</select>

on the option with id of "createCatagory" a pop up opens and allows the creation of a new category folder on the server using an ajax call as follows:

<script type="text/javascript">

$(document).ready(function() {


        $("#vidcatBut").click(function()    {

//  event.preventDefault();


        var albumName = $("#albumName").val();


        $.post("includes/createAlbum.php",{albumName: albumName}, function(json)
         {

            if(json.result === "success") {

        $("#vidcatResult").html( "The Album "+albumName+"has been Created!");


    }else{
        $("#vidcatResult").html(json.message);
    }   

         })

$('#choosevidCat').load(location.href + "#choosevidCat");

    });//click function
});//ready function
</script>

What I would like to happen is when the create button is clicked that the select list will reload and reflect the new category in the drop down list without the need to refresh the page after the pop up box is closed. is this possible please

I have a drop down list that is dynamically filled with categories using php embeded in the html as follows:

<select name="select" id="choosevidCat" required class="choosevidCat">
<option>Choose the Catagory for your Video Here</option>
            <?php 
    $sql = ("SELECT albumId, albumName FROM albums");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($mysqli, $sql);

while($row = mysqli_fetch_array($result)) {
$albumid = ($row['albumId']);
$album_name = ($row['albumName']);
echo "<option value=".$albumid. ">$album_name</option>";
}

?>


<option id="createCat" value="createCatagory">Create New Catagory Here</option>
</select>

on the option with id of "createCatagory" a pop up opens and allows the creation of a new category folder on the server using an ajax call as follows:

<script type="text/javascript">

$(document).ready(function() {


        $("#vidcatBut").click(function()    {

//  event.preventDefault();


        var albumName = $("#albumName").val();


        $.post("includes/createAlbum.php",{albumName: albumName}, function(json)
         {

            if(json.result === "success") {

        $("#vidcatResult").html( "The Album "+albumName+"has been Created!");


    }else{
        $("#vidcatResult").html(json.message);
    }   

         })

$('#choosevidCat').load(location.href + "#choosevidCat");

    });//click function
});//ready function
</script>

What I would like to happen is when the create button is clicked that the select list will reload and reflect the new category in the drop down list without the need to refresh the page after the pop up box is closed. is this possible please

Share Improve this question asked Apr 23, 2015 at 23:59 TinyTiny 3632 gold badges6 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use jQuery's append to append your new category within the container that contains your categories.


An example:

$(".categoryContainer").append('<div class="category">My new Category</div>');

You can easily modify the above to append to your option list instead of a container

Note: I suggest you do this on the success callback of your AJAX call to make sure that it has already been saved on your server before appending.

$("#appendBtn").click(function() {
  $(".container").append("<div class='category'>New Category</div>");

})
.container {
  margin: 0 auto;
  width: 320px;
  height: auto;
  padding-bottom: 64px;
  background-color: #999;
}
#appendBtn {
  display: block;
  margin: 12px auto;
  padding: 12px;
  font-size: 14px;
  cursor: pointer;
}
.category {
  width: 90%;
  height: 32px;
  margin: 0 auto;
  line-height: 32px;
  text-align: center;
  background-color: #eee;
  border: 1px solid red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="category">Category</div>
  <div class="category">Category</div>
  <div class="category">Category</div>
</div>

<button id="appendBtn">Append Category</button>

You could remove all the current options using jQuery's .empty() and then load the contents of the select box in the POST success callback like this:

$.post("includes/createAlbum.php",{albumName: albumName}, function(json)
{

    if(json.result === "success") {

        $("#vidcatResult").html( "The Album "+albumName+"has been Created!");

        // First remove all the existing options
       $('#choosevidCat').empty();

        // Load the content:
        $('#choosevidCat').load(location.href + "#choosevidCat > *");

    } else {
        $("#vidcatResult").html(json.message);
    }   

})

Using #choosevidCat > * in the load() function adds all of #choosevidCat's children (all of the options) into the select box. Otherwise (using just #choosevidCat), jQuery will add the select box into the existing select box, like this:

<select name="select" id="choosevidCat">

    <select name="select" id="choosevidCat">

        ...

    </select>

</select>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742407415a4438133.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信