2016-10-28 34 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() –