2

我无法访问输入 checkbox_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']); 
}
4

1 回答 1

1

括号是名称的一部分:

var query_string = '';
$("input[name='checkbox_material[]']").each(function() {
    if (this.checked) {
        query_string += "&checkbox_material[]=" + this.value;
    }
});
于 2013-05-12T19:38:15.923 回答