[英]Removing one option from select dropdown generated by jquery
I am working on a very basic administrator functionality of a social network and I came across this issue of not being able to remove an option from select dropdown list that I previously generated using jquery. 我正在研究社交网络的一个非常基本的管理员功能,并且遇到了无法从以前使用jquery生成的选择下拉列表中删除选项的问题。 The dropdown list contains all users of the social network. 下拉列表包含社交网络的所有用户。 Administrator upon clicking on "Delete account" deletes the corresponding record from the database. 管理员单击“删除帐户”后,将从数据库中删除相应的记录。
Now the question being - when I click on "delete account" it works perfectly fine but the option with a username is still there in a dropdown list and is possible to be picked - when picked it obviously returns dozens of PHP warnings and errors because the record is not in a database anymore. 现在的问题是-当我单击“删除帐户”时,它工作得很好,但是带有用户名的选项仍在下拉列表中,并且可以选择-选择时,它显然会返回数十个PHP警告和错误,因为记录不再在数据库中。 How can I remove this option straight away? 如何立即删除此选项? I tried something like the following, but it doesn't work. 我尝试了类似以下的方法,但是它不起作用。
admin_panel.php (only relevant stuff) admin_panel.php (仅相关内容)
<select name='users' id='users'>
<option value="" disabled selected>Select user</option>
<?php
$sql = mysql_query("SELECT * FROM users WHERE id <>'".$_SESSION['user_id']."'ORDER BY username DESC") or die(mysql_error());
$userList = [];
while($row=mysql_fetch_assoc($sql)){
$username = $row['username'];
$userID = $row['id'];
$userList .= '<option name="userID" value='.$userID.'>'.$username.'</option>';
}
echo $userList;
?>
</select></br></br></div>
<div id="user_info">
<!-- generated user info table-->
</div>
<script type="text/javascript">
"$('#user_info').on('click', '#deleteAccount', function(e){
data.command = 'deleteAccount'
data.userID = $('#users').val()
$.post(theURL, data, function(result){
//Do what you want with the response.
$('#delete_account_success').html(result);
})
$("#users option[value='data.userID']").remove();
$('#delete_account_success').show();
$('#delete_account_success').fadeOut(5000);
})
</script>
processUser.php (part of a switch statement) processUser.php (switch语句的一部分)
if(isset($_POST['command'])){
$cmd = $_POST['command'];
$userID = $_POST['userID'];
$sql=mysql_query("SELECT * FROM users WHERE id='".$userID."'");
$userData = [];
while($row = mysql_fetch_assoc($sql)){
$userData['userid'] = $row['id'];
$userData['username'] = $row['username'];
$userData['name'] = $row['name'];
$userData['date'] = $row['date'];
$userData['email'] = $row['email'];
$userData['avatar'] = $row['avatar'];
$userData['about'] = $row['about'];
$userData['admin'] = $row['admin'];
}
switch($cmd){
case 'deleteAccount':
$sql= "DELETE FROM users WHERE id =".$userID;
$result=mysql_query($sql);
echo "<img src='pics/ok.png' class='admin_updated_ok'>";
break;
}
On this line 在这条线上
$("#users option[value='data.userID']").remove();
You're removing any option items from #users where the value is equal to the string literal data.userID
您正在从#users中删除所有值等于字符串文字data.userID
选项项。
Try changing it to 尝试将其更改为
$("#users option[value='" + data.userID + "']").remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.