1

我在以下代码中获取复选框值时遇到问题(没有页面加载)。如果我单击复选框,则可以通过数组变量传递值check。但是每当我单击复选框时,我都会得到" Array ( [0] => undefined )".

这是我的代码:

索引.html

<script src="create_ajax_object.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function display() {
    var ajax = create_ajax_object();
    if (ajax) {
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                document.getElementById("target").innerHTML = ajax.responseText;
            }
        }
        ajax.open("POST", "fetchdata.php", true);
        var check = new Array();
        var "check[]=" + encodeURIComponent(document.myForm.user.value);
        ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        ajax.send(check[]);
    } else {
        alert("Your browser doesnt support AJAX!");
    }
} </script>
<br>
<form name='myForm'>
    <input type="checkbox" name="user" value="First" onclick="display();">1
    <br>
    <input type="checkbox" name="user" value="Second" onclick="display();">2
    <br>
    <input type="checkbox" name="user" value="Third" onclick="display();">3
    <br>
</form>

</html>

获取数据.php

<?php
$no=array($_POST['check']);
foreach($no as $n)
{
  print_r($n);
}   
?>
4

1 回答 1

0

该变量document.myForm.user是复选框对象的数组,您必须对其进行迭代并检查checked每个元素的属性。

于 2013-08-16T07:50:58.823 回答