-1

如果这是一个简单的解决方法,我深表歉意,我对 jquery 有点陌生。首先,我不认为这是我最初的调用搞砸了,因为如果我放入一个简单的警报函数,它会在点击时正常工作,基本上当<li>点击它时,jquery 会触发一个 ajax 到 php 函数发送类到<li>php 脚本并返回,打开带有结果的警报。据我所知,它应该可以工作,但我的知识还是相当有限的。任何更改任何人都可以看看我有什么,看看你是否可以清理它,目前点击时没有任何反应,控制台中甚至没有出现错误。但我想即使是我的 ajax 调用也是错误的。任何想法为什么点击时什么都没有发生?

HTML:

  <li title = "Previous Month"  id = "changeMonthBack" class = "<?php echo date('n'); ?>"><img src="images/leftArrow.gif" width="54" height="45" alt="previous month"></li>

jQuery/javascript:

//javascript document

$(document).ready(function(){
//need to do an onclick for getting the new month(previous)
$(".changeMonthBack").click(function(){
  function calendar(){
    //set the id for php
    var identifier = 1;
    //get the class to use for month
        var str = document.getElementById("changeMonthBack").class;
    //get the month number
    //if the month is 1, we obviously need it to be 12
    if str == 1{
        var month = 12;
    }
    else{
        var month = str - 1;
    }
      $.post("redraw.php"),{
        identifier: identifier,
        month: month
      },
        function(data,status){
          alert("Data: " + data + "\nStatus: " + status);
  };
})//end function 1

});

4

1 回答 1

2

脚本存在多个问题。
1. 正如 Esthete 建议的那样,选择器应该是一个 id 选择器#changeMonthBack
2. 您正在创建一个名为calendar但从未调用它的闭包方法
3. 存在多个语法错误(使用像spket这样的 javascript 编辑器)

您正在创建一个名为calendar但从不调用它的函数。

$(document).ready(function() {
            // need to do an onclick for getting the new month(previous)
            $("#changeMonthBack").click(function() {
                        // set the id for php
                        var identifier = 1;
                        // get the class to use for month
                        var str = parseInt($(this).attr('class'),10);
                        // get the month number
                        // if the month is 1, we obviously need it to be 12
                        if (str == 1) {
                            var month = 12;
                        } else {
                            var month = str - 1;
                        }
                        $.post("redraw.php", {
                                    identifier : identifier,
                                    month : month
                                },

                                function(data, status) {
                                    alert("Data: " + data + "\nStatus: "
                                            + status);
                                });
                    });
        });
于 2013-02-18T04:18:41.340 回答