[英]Jquery Can't append option Filed in Select
我在将<option>
附加到<select>
时遇到问题,因为我的情况是如果用户要添加自己的选项,则显示所有列表,因此我正在打开模型并从用户那里获取输入
问题是我无法将用户输入与其指定的“选择”选项相加
因为我有更多选择
HTML:
<li style="width: 27%;">
<label class="select">
<span style="display: inline-block;padding: 10px 0;">Institute:</span>
<select name="institute[]" id="institue1" onchange="Myinsi(this.value, 'institue1')" class="validate[required] institute" style="width: 65%;float: right;">
<option value="">Select</option>
<?php
$sel = "select * from institute where `status`= 'Y'";
$res = mysql_query($sel);
while ($row = mysql_fetch_assoc($res)) {
echo "<option value='{$row['title']}'>{$row['title']}</option>\n";
}
echo'<option value="ShowOtherIns" id="ShowOtherIns">Other</option>';
?>
</select>
<i></i>
</label>
</li>
jQuery代码:
<script>
function Myinsi(value, id) {
console.log(value + '----' + id);
var index = $(id + ' option').length + 1;
if (value == "ShowOtherIns") {
$('#OtherIns').modal("show");
document.getElementById("btnSave").onclick = function () {
var NewText = document.getElementById('OtherInsTextID').value;
if (NewText = !'') {
var element = document.getElementById(id); // assuming ul exists
$(id).append('<option value="' + NewText + '" selected="selected">' + NewText + '</option>');
$('#OtherIns').modal("hide");
} else {
console.log("text = exibir menu");
}
};
}
}
</script>
我也附上我的屏幕快照
jQuery选择器不正确。 您必须输入“#”,后跟html元素的ID。 在行var index = $(id + ' option').length + 1;
你应该写var index = $('#'+id + ' option').length + 1;
。
同样在$(id).append('<option value="' + NewText + '" selected="selected">' + NewText + '</option>');
您应该编写$('#'+id).append('<option value="' + NewText + '" selected="selected">' + NewText + '</option>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.