Javascript / AJAX ReferenceError:x未定义

时间:2022-11-05 14:30:33

I have a jQuery function that takes the value from a PHP-generated check box and sends it over AJAX. The value is always a single word with nothing but letters by the way. Below is the script.

我有一个jQuery函数,它从PHP生成的复选框中获取值并通过AJAX发送它。这个值总是一个单词,顺便说一句,只有字母。下面是脚本。

<script type="text/javascript">
    $(document).ready(function() {
        $("input:checkbox").on("click", function () {
            step = this.value;
            //document.getElementById("test").innerHTML = step;
            responseArray = [];

            xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    responseArray = eval("(" + xmlhttp.responseText + ")");
                    document.getElementById("test").innerHTML = responseArray;
                }
            }

            xmlhttp.open("GET", "checkbox.php?step="+step, true);
            xmlhttp.send();
        });
    });
</script>

The above code results in the "ReferenceError: [this.value] is not defined". [this.value] is the actual value though which changes based on the box that is checked. If you notice the 5th line in the above code, when I don't comment that line out it displays the correct value in "test" for the step variable, so it's something after that. Below is the checkbox.php file completely simplified down to basically nothing and it still causes the error.

上面的代码导致“ReferenceError:[this.value]未定义”。 [this.value]是实际值,但会根据选中的框进行更改。如果您注意到上面代码中的第5行,当我不注释该行时,它会在步骤变量的“test”中显示正确的值,所以它就是之后的内容。下面是checkbox.php文件完全简化为基本没什么,它仍然导致错误。

<?php       
    $step = $_GET["step"];
    echo "[" . $step . "]";
?>

1 个解决方案

#1


3  

As I can see your using Jquery, you should use JQuery's AJAX object. It simplifies the request a lot:

正如我可以看到你使用Jquery,你应该使用JQuery的AJAX对象。它大大简化了请求:

$.ajax({
  url: 'checkbox.php?step=',
  success: function(data) {
    alert(data);
  }
});

http://api.jquery.com/jQuery.ajax/

Now your problem seems to be that you're not getting the value of the checkbox through properly. Checkboxes are either on or off. Here's how to get the various parameters:

现在您的问题似乎是您没有正确获取复选框的值。复选框打开或关闭。以下是获取各种参数的方法:

$(document).ready(function () {
    $("input:checkbox").on("click", function () {
        var checkboxID = $(this).attr("ID");        // Returns the ID of the checkbox
        var isChecked = $(this).is(':checked');     // Returns true or false

        alert($(this).attr("ID") + " is " + isChecked);
    });
});

So your final code might look something like:

所以你的最终代码可能如下所示:

$(document).ready(function () {
    $("input:checkbox").on("click", function () {
        var checkboxID = $(this).attr("ID");        // Returns the ID of the checkbox
        var isChecked = $(this).is(':checked');     // Returns true or false

        $.ajax({
           url: 'checkbox.php?step=' + isChecked ,
           success: function(data) {
             alert(data);
           }
         });
    });
});

(untested code) which would send a request to the url checkbox.php?step=true

(未经测试的代码)会向url发送请求checkbox.php?step = true

And hello from 2+2 :D

你好,从2 + 2:D

#1


3  

As I can see your using Jquery, you should use JQuery's AJAX object. It simplifies the request a lot:

正如我可以看到你使用Jquery,你应该使用JQuery的AJAX对象。它大大简化了请求:

$.ajax({
  url: 'checkbox.php?step=',
  success: function(data) {
    alert(data);
  }
});

http://api.jquery.com/jQuery.ajax/

Now your problem seems to be that you're not getting the value of the checkbox through properly. Checkboxes are either on or off. Here's how to get the various parameters:

现在您的问题似乎是您没有正确获取复选框的值。复选框打开或关闭。以下是获取各种参数的方法:

$(document).ready(function () {
    $("input:checkbox").on("click", function () {
        var checkboxID = $(this).attr("ID");        // Returns the ID of the checkbox
        var isChecked = $(this).is(':checked');     // Returns true or false

        alert($(this).attr("ID") + " is " + isChecked);
    });
});

So your final code might look something like:

所以你的最终代码可能如下所示:

$(document).ready(function () {
    $("input:checkbox").on("click", function () {
        var checkboxID = $(this).attr("ID");        // Returns the ID of the checkbox
        var isChecked = $(this).is(':checked');     // Returns true or false

        $.ajax({
           url: 'checkbox.php?step=' + isChecked ,
           success: function(data) {
             alert(data);
           }
         });
    });
});

(untested code) which would send a request to the url checkbox.php?step=true

(未经测试的代码)会向url发送请求checkbox.php?step = true

And hello from 2+2 :D

你好,从2 + 2:D