gpt4 book ai didi

php - jQuery、Ajax、PHP 两个复选框

转载 作者:行者123 更新时间:2023-11-28 01:56:49 25 4
gpt4 key购买 nike

我无法访问输入复选框 Material 。我的代码有问题。请帮我。我的 textarea 工作正常,但 checkbox_material 从未开始设置。请帮我弄清楚问题是什么。

我的表单:

<div class="form_style">
<div id="checkbox_content"></div>
<input type="checkbox" value="1" alt="Checkbox" name="checkbox_material[]">One
<input type="checkbox" value="2" alt="Checkbox" name="checkbox_material[]">Two<br>
<textarea id="materialMessage" rows="5" cols="45" name="material_message"></textarea<br>
<button id="materialSubmit">Add record</button>
</div>

Javascript:

<script type="text/javascript">
$(document).ready(function() {
$("#materialSubmit").click(function (e) {
e.preventDefault();
if($("#materialMessage").val() === "") {
alert("Please enter some text!");
return false;
}


var query_string = '';
$("input[@type='checkbox'][@name='checkbox_material']").each(function() {
if (this.checked) {
query_string += "&checkbox_material[]=" + this.value;
}
});

if(query_string.length == 0) {
alert("Please enter some HELLO!");
return false;
}

alert("Hej");
$.ajax({
type: "POST",
url: "material.php",
data: "id=1" + query_string,
success:function(t) {
$("div#checkbox_content").empty().append(t);
},
error:function() {
$("div#checkbox_content").append("An error occured during processing");
}
});




var myData = "material_message="+ $("#materialMessage").val();
jQuery.ajax({
type: "POST",
url: "material.php",
dataType:"text",
data:myData,
success:function(response) {
$("#materialResponds").append(response);
$("#materialMessage").val('');
},
error:function (xhr, ajaxOptions, thrownError) {
alert(thrownError); //throw any errors
}
});
});

PHP:

if (isset($_POST['checkbox_material'])) 
{
print_r($_POST['checkbox_material']);
}

最佳答案

括号是名字的一部分:

var query_string = '';
$("input[name='checkbox_material[]']").each(function() {
if (this.checked) {
query_string += "&checkbox_material[]=" + this.value;
}
});

关于php - jQuery、Ajax、PHP 两个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16511104/

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