下面给出了我编写的用于从下拉菜单中获取用户选择的类代码和部分值的代码片段。 但我只获得了选择的第一个元素,而不是获取所选元素。未从选择标记中获取所选项目
HTML代码:
<div id="dialog-form" title="Create New Academic Year">
<p class="validateTips">All form fields are required.</p>
<fieldset>
<label for="class_code">Class</label>
<select name="class_code" id="class_code" class="text ui-widget-content ui-corner-all">
<?php
include_once 'config.php';
$sql = "select class_code from list_class";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['class_code'] ?>"><?php echo $row['class_code'] ?></option>
<?php
}
?>
</select>
<label for="section">Section</label>
<select name="section" id="section" class="text ui-widget-content ui-corner-all" multiple>
<?php
include_once 'config.php';
$sql = "select section_code from list_sections";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['section_code'] ?>"><?php echo $row['section_code'] ?></option>
<?php
}
?>
</select>
</fieldset>
</div>
JS代码:
var new_dialog = function (type, row) {
var dlg = $("#dialog-form").clone();
type = type || 'Create';
var config = {
autoOpen: true,
height: 300,
width: 350,
modal: true,
buttons: {
"Insert": function() {
//To get the class_code value
alert($('select[name="class_code"]').val());
//To get the section value
alert($('select[name="section"]').val());
},
"Cancel": function() {
dlg.dialog("close");
}
},
close: function() {
dlg.remove();
}
};
dlg.dialog(config);
};
$("#create-user").button().click(new_dialog);
我甚至尝试
alert($('#class_code option:selected')val());
但是,即使这没有奏效。 有人可以帮我解决我哪里出错的地方。 在此先感谢。
你想从下拉选中右边的值? – Sand
是的,我想要选择的值 –
您想将该值发送给PHP吗? – Sand