2009-11-10 43 views
0

工作隨着the help of members from this post,我從原型轉換到jQuery的。jQuery的更新不與IE7/8

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,但更新適用於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')); 
    } 

模型的模糊是以下。

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> 
+0

在哪裏/什麼是id爲'ajax_msg'元素? – 2009-11-10 16:12:38

回答

3

我不認爲IE支持document.getElementByClassName( 「」)

嘗試用替換

allNodes = document.getElementsByClassName("process"); 

allNodes = $(".process"); 

這是JQuery的方式。

+1

+1 - IE不好,很好。 – 2009-11-10 16:28:31

+0

謝謝Odge。就是這樣。 – shin 2009-11-10 17:04:45

0

在IE8只需打開Web開發工具包,並把一個破發點,在這條線:

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

我也將在.ajax調用放在這裏一個破發點:

var params = 'ids='+parameter_string; 

    $.ajax({ 

然後走着瞧吧。

這種方式可以確保您正確地使用它,否則在您到達此處之前,IE可能有問題,或者,如果您到達此處,則需要查看它是否發送到服務並返回,這是第一個網址會告訴你什麼。然後單步執行success的代碼,看看它是否因某個錯誤而跳出某個點。

一旦你知道一個事實,即這個代碼,那麼你可以用你所學的知識更新問題,但我已經在IE7中使用該功能,8所以它應該工作的罰款。