admin管理员组文章数量:1336624
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 badges2 Answers
Reset to default 3You 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 option
s 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>
本文标签: javascriptreloading a select drop down box without refreshing the pageStack Overflow
版权声明:本文标题:javascript - reloading a select drop down box without refreshing the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742407415a2469107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论