gpt4 book ai didi

php - 使用jquery获取选择元素的值

转载 作者:行者123 更新时间:2023-12-01 03:08:10 25 4
gpt4 key购买 nike

我有以下 HTML

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

简而言之,有一个名为 type[] 的选择标签和一个名为 questions[] 的文本输入字段。我需要将值作为 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。它仅发送第一个问题,而不发送数组。我想要数组中的值,这样我就可以在循环中进行插入。

最佳答案

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

关于php - 使用jquery获取选择元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com