2017-09-27 66 views
0

下面给出了我编写的用于从下拉菜单中获取用户选择的类代码和部分值的代码片段。 但我只获得了选择的第一个元素,而不是获取所选元素。未从选择标记中获取所选项目

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()); 

但是,即使这没有奏效。 有人可以帮我解决我哪里出错的地方。 在此先感谢。

+0

你想从下拉选中右边的值? – Sand

+0

是的,我想要选择的值 –

+0

您想将该值发送给PHP吗? – Sand

回答

2

你不必选择所选选项 - 你只需要选择选择框本身

alert($('#class_code').val()); 

在你的情况,有一个更多的问题。您克隆了表单,因此每个选择框在dom中都存在多次,并且val()仅返回第一个选择框的选定值,该选择框是列表中的第一个元素。

你必须选择 - 摆脱克隆或尝试这样的事情

alert($('select[name="class_code"]', dlg).val()); 

添加DLG上下文使得克隆的元素中jQuery的搜索,我们应该解决您的问题

+0

仍只获得第一个元素。 –

+0

你是否舒服,只有一个ID为'class_code'的元素?我提供的代码应该返回选定的选项值 – Philipp

+0

是的。此ID不存在其他元素。 –

0

正如我我看到您正在尝试获取多个选定值的选择不具有“多个”属性。

请看这里:

<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> 

尝试加入 “多” 在这里:

<select name="class_code" id="class_code" class="text ui-widget-content ui-corner-all" multiple> 
       //ur code 
</select> 
+0

添加多个给我“空”作为输出 –