2017-09-27 18 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()はリストの最初の要素である最初の選択ボックスの選択値のみを返します。オプションにあなたが持っている

- クローンを取り除くか、コンテキストにDLGを追加するこの

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

ような何かをしようとしますが、クローン化された要素内にjqueryの検索を行い、どのような私としては、あなたの問題

+0

まだ最初の要素だけを取得しています。 –

+0

あなたはshureですか、idが 'class_code'の要素は1つだけですか?私が提供したコードは、選択されたオプションの値を返す必要があります – 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

複数を追加すると出力として「null」が返されます –

関連する問題