2016-10-28 26 views
1

選擇元素的值我有以下HTML獲取使用jQuery

<div class="question-set"> 
     <div class="row question"> 
     <div class="col-md-2"> 
      <select class="form-control" name="type[]"> 
      <option value="Teacher feedback">Teacher feedback</option> 
      <option value="Genral Question">Genral Question</option> 
      <option value="Informative Question">Informative Question</option> 
      </select> 
     </div> 
     <div class="col-md-8"> 
      <input type="text" class="form-control" name="questions[]" placeholder="Enter the question here" /> 
     </div> 
     </div> 
     <div class="row question"> 
     <div class="col-md-2"> 
      <select class="form-control" name="type[]"> 
      <option value="Teacher feedback">Teacher feedback</option> 
      <option value="Genral Question">Genral Question</option> 
      <option value="Informative Question">Informative Question</option> 
      </select> 
     </div> 
     <div class="col-md-8"> 
      <input type="text" class="form-control" name="questions[]" placeholder="Enter the question here" /> 
     </div> 
     </div> 
    </div> 

總之有與名稱類型[]和文本輸入字段名稱問題[]一個選擇標記。我需要將這些值作爲ajax調用插入到數據庫中傳遞給一個php頁面。我有以下的AJAX代碼..

$.ajax({ 
      method: "POST", 
      url: "feedback_create_form_process.php", 
      data: {"questions": $("input[name='questions[]']").val(), "type": $("select option:selected").val()}, 
      success: function(data){ 
      alert(data); 
      } 
     }); 

這是我的PHP代碼

<?php 
    include 'con.php'; 
    if (isset($_POST) && sizeof($_POST)) { 
    $question = $_POST['questions']; 
    $type = $_POST['type']; 

    echo count($question); 
    foreach($question as $key => $n) { 
     $result = $con->query("insert into form question(null, ".$question[$key].", ".$n.")"); 
    } 
    } 
?> 

爲1只發送的第一個問題,而不是數組時,返回計數。我想要一個數組中的值,所以我可以在一個循環中插入。

回答

2

如JQuery的.VAL()有記錄

Description: Get the current value of the first element in the set of matched elements.

您可以使用

var values = $("input[name='questions[]']").map(function(){return $(this).val();}).get(); 

var values = []; 
$("input[name='questions[]']").each(function() { 
    values.push($(this).val()); 
}); 
+0

我對select元素做同樣的事嗎? –

+0

是的,與任何jquery選擇器(y) –

+0

@CastorCGodinho檢查此http://stackoverflow.com/a/19372680/1475257 –

0

您可以使用jQuery的map()函數的返回值一個數組,如下所示:

var questions = $("input[name='questions[]']").map(function() {return $(this).val()}).get(); 
var types = $("select[name='type[]']").map(function() {return $(this).val()}).get(); 

$.ajax({ 
    method: "POST", 
    url: "feedback_create_form_process.php", 
    data: {"questions": questions, "type": types}, 
    success: function(data){ 
     alert(data); 
    } 
}); 

編輯:實現類似的解決方案由ABDU_GO提供。我發現我的腳本更具可讀性,但如果您發現這是您的解決方案,我建議您接受他的答案。

+0

地圖功能是jQuery像魅力一樣工作,但是當我用數據JSON數組傳遞它時,AJAX調用似乎不起作用。 –

+0

你有什麼錯誤,如果有的話,你在控制檯? – Santi

+0

不推薦使用getPreventDefault()。使用defaultPrevented instead.feedback_create_form.php TypeError:對未實現接口HTMLInputElement的對象調用'stepUp'。 r.param/E() jquery.min.js:4 XB() jquery.min.js:4個 XB() jquery.min.js:4個 XB() jquery.min.js :4 XB() jquery.min.js:4 r.param() jquery.min.js:4 阿賈克斯() jquery.min.js:4 feedback_create_form.php:28 r.event.dispatch() jquery.min.js:3 r.event.add/q.handle() –