【问题标题】:jQuery update does not work with IE7/8jQuery 更新不适用于 IE7/8
【发布时间】:2010-12-15 02:33:15
【问题描述】:

使用the help of members from this post,我从原型转换为jQuery。

function jsUpdateCart(){
  var parameter_string = '';
  allNodes = document.getElementsByClassName("process");
  for(i = 0; i < allNodes.length; i++) {
    var tempid = allNodes[i].id;
    var temp = new Array;
    temp = tempid.split("_");
    var real_id = temp[2];
    var real_value = allNodes[i].value;
    parameter_string += real_id +':'+real_value+',';
  }

  var params = 'ids='+parameter_string;

   $.ajax({
   type: "POST",
   url: "http://127.0.0.1/codeigniter_shopping/index.php/welcome/ajax_cart",
   data: params,
   success: function( r ) {
    $('#ajax_msg').html( r );
    location.reload( true );
  }
 });

}



function jsRemoveProduct(id){
  var params = 'id='+id;
  $.ajax({
   type: "POST",
   url: "http://127.0.0.1/codeigniter_shopping/index.php/welcome/ajax_cart_remove",
   data: params,
   success: function( r ) {
    $('#ajax_msg').html( r );
    location.reload( true );
  }
 });
}

ajax_cart_remove 适用于 Firefox 和 IE,但 update 适用于 Firefox,但不适用于 IE 7/8。

谁能给我一些建议。

你可以看到原来的prototype code here

控制器中的ajax_cart和ajax_cart_remove如下。

function ajax_cart(){
    $this->MOrders->updateCartAjax($this->input->post('ids'));  
  }

  function ajax_cart_remove(){
    $this->MOrders->removeLineItem($this->input->post('id'));
  }

MODers 的模型如下。

function removeLineItem($id){
    $id = id_clean($id);
    $totalprice = 0;
    $cart = $_SESSION['cart'];
    if (isset($cart[$id])){
        unset($cart[$id]);
        foreach ($cart as $id => $product){
            $totalprice += $product['price'] * $product['count'];
        }       
        $_SESSION['totalprice'] = $this->format_currency($totalprice);
        $_SESSION['cart'] = $cart;

        echo "Product removed.";
    }else{
        echo "Product not in cart!";
    }
}

function updateCartAjax($idlist){
    $cart = $_SESSION['cart'];
    $records = explode(',',$idlist);
    $updated = 0;
    $totalprice = $_SESSION['totalprice'];

    if (count($records)){
        foreach ($records as $record){
            if (strlen($record)){
                $fields = explode(":",$record);
                $id = id_clean($fields[0]);
                $ct = $fields[1];

                if ($ct > 0 && $ct != $cart[$id]['count']){
                    $cart[$id]['count'] = $ct;
                    $updated++;
                }elseif ($ct == 0){
                    unset($cart[$id]);
                    $updated++;
                }
            }   
        }

        if ($updated){
            $totalprice=0;
            foreach ($cart as $id => $product){
                $totalprice += $product['price'] * $product['count'];
            }       

            $_SESSION['totalprice'] = $this->format_currency($totalprice);
            $_SESSION['cart'] = $cart;

            switch ($updated){
                case 0:
                $string = "No records";
                break;

                case 1:
                $string = "$updated record";
                break;

                default:
                $string = "$updated records";
                break;
            }
            echo "$string updated";

        }else{
            echo "No changes detected";

        }
    }else{
        echo "Nothing to update";

    }
}

以下是表单的html输出

<form action="http://127.0.0.1/codeigniter_shopping_copy2/index.php/welcome/checkout" method="post"><table border='1' cellspacing='0' cellpadding='5'>
<tr valign='top'>
<td><input type="text" name="li_id[10]" value="1" id="li_id_10" class="process" size="5"  /></td>
<td id='li_name_10'>Dress 1</td>
<td id='li_price_10'>33.95</td>
<td id='li_total_10'>33.95</td>
<td><input type='button' name='delete' value='delete' onclick='jsRemoveProduct(10)'></td>
</tr>
<tr valign='top'>

<td><input type="text" name="li_id[6]" value="2" id="li_id_6" class="process" size="5"  /></td>
<td id='li_name_6'>Shoes 1</td>
<td id='li_price_6'>23.95</td>
<td id='li_total_6'>47.90</td>
<td><input type='button' name='delete' value='delete' onclick='jsRemoveProduct(6)'></td>
</tr>
<tr valign='top'>
<td colspan='3'>&nbsp;</td>
<td colspan='2'>81.85 
<input type="hidden" name="name" value="total" />
<input type="hidden" name="id" value="total" />
<input type="hidden" name="value" value="81.85" />
</td>
</tr>

<tr valign='top'>
<td colspan='3'>&nbsp;</td>
<td colspan='2'><input type='button' name='update' value='update' onclick='jsUpdateCart()'/></td>
</tr>
<tr valign='top'>
<td colspan='3'>&nbsp;</td>
<td colspan='2'><input type="submit" name="submit" value="checkout"  /></td>
</tr>
</table>
</form>

【问题讨论】:

  • id为ajax_msg的元素在哪里/是什么?

标签: php jquery codeigniter


【解决方案1】:

我认为 IE 不支持 document.getElementByClassName("")

尝试替换

allNodes = document.getElementsByClassName("process");

allNodes = $(".process");

wich 是 JQuery 方式。

【讨论】:

    【解决方案2】:

    在 IE8 中只需打开 Web Developer Toolkit 并在此行设置一个断点:

    $('#ajax_msg').html( r );
    

    我也会在 .ajax 调用这里设置一个断点:

    var params = 'ids='+parameter_string;
    
       $.ajax({
    

    然后看看会发生什么。

    这样您可以确保您正确访问它,否则 IE 在您到达之前可能会出现问题,或者,如果您到达这里,那么您需要查看它是否到达服务并返回,这就是第一个网址会告诉你。然后单步执行success 代码,看看它是否由于错误而在某个时候跳出。

    一旦你知道这段代码是问题所在,你就可以用你学到的东西更新,但我在 IE7 和 8 中使用过这个函数,所以它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      相关资源
      最近更新 更多