即使我选择了有效选项
,选择也是未定义的 <div class="form-group">
<label for="rights">Rights</label>
<select class="form-control" id="rs">
<option>Alege...</option>
<?php $reqRights = $conn->query("SELECT `IdRights`,`TitleRights` FROM Rights");
while ($rowR = $reqRights->fetch_assoc()) { ?>
<option value="<?php echo $rowR['IdRights']?>"><?php echo $rowR['TitleRights']?></option>
<?php } ?>
</select>
</div>
<script>
<!--Start Update User by Admin-->
function updatedata(str){
var id = str;
var username = $('#username'+str).val();
var email = $('#email'+str).val();
var name = $('#name'+str).val();
var surname = $('#surname'+str).val();
var rights = $('#rs option:selected').val();
var datas="username="+username+"&email="+email+"&name="+name+"&surname="+surname+"&rights="+rights;
$.ajax({
type: "POST",
url: "app/admin_update_data_user.php?id="+id,
data: datas
}).done(function( data ) {
$('#admininfo').html(data);
viewdata();
});
}
<!--End Update User by Admin-->
</script>
结果是:username = Admin&amp; email=admin@ex.com& name = Admin&amp; surname = Admin&amp; rights = undefined
我不明白为什么有利的结果......
提前谢谢!
答案 0 :(得分:0)
此代码将显示undefined
作为第一个选项。因为该选项没有价值。
var rights = $('#rs option:selected').val();
alert(rights)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control" id="rs">
<option>Alege...</option>
<option value="1">Admin</option>
<option value="2">Manager</option>
</select>