下面給出了我編寫的用於從下拉菜單中獲取用戶選擇的類代碼和部分值的代碼片段。 但我只獲得了選擇的第一個元素,而不是獲取所選元素。未從選擇標記中獲取所選項目
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