2011-09-26 68 views
1

我試過Google搜索,但它只有與jQuery noconflict選項。原型有沒有衝突選項像jQuery?

我會使用這個,但我的網站是非常jQuery沉重,它將需要一個白色,以及我添加的原型代碼可能是暫時的,只有幾行。

如果沒有原型沒有衝突選項如何轉換下面的代碼,我的javascript代碼是有限的?

// JavaScript Document 
// calculate price based on quantity 
function changeQty(change){ 
var currentQty = parseInt($F('quant')) // Where quant is the id of your quantity input field. Gets value of currentQty field 

switch (change) { 
    case 'add': 
     currentQty += 1 
     $('quant').value = currentQty 
     calculate() 
     break 
    case 'subtract': 
     if (currentQty > 1) { // only subtract if qty is greater than zero 
      currentQty -= 1 
      $('quant').value = currentQty 
      calculate() 
     } 
     break 
    case 'field': 
     if (currentQty > 0) { 
      window.setTimeout('calculate()', 500) 
     } 
     break 
} 
} 
function calculate(){ 
var currentQty = parseInt($F('quant')) // Where quant is the id of your quantity input field. Gets value of currentQty field  
var jsnormalprice = $F('jsnormalprice') // Where jsnormalprice is the id of your hidden base price field. Gets value of base_price field  
var jsspecialprice = $F('jsspecialprice') // Where is the id of your hidden base price field. Gets value of base_price field 

if (currentQty > 0) { // Don't want price to display if zero if customer zeros out quantity 
    var new_jsnormalprice = jsnormalprice * currentQty // Calculate the price.  
    var new_jsnormalprice = new_jsnormalprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down. 

    var new_jsspecialprice = jsspecialprice * currentQty // Calculate the price.   
    var new_jsspecialprice = new_jsspecialprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down. 

} else { // set price back to original price 
    new_jsnormalprice = jsnormalprice 
    new_jsspecialprice = jsspecialprice 
} 

$('jsnormalpriceshow').update(new_jsnormalprice) // Where jsnormalpriceshow is the id of your span for the echoed product price 
$('jsspecialpriceshow').update(new_jsspecialprice) // Where jsspecialpriceshow is the id of your span for the echoed product price 

} 

回答

3

原型沒有一個沒有衝突模式..

我已經轉換你的代碼,但我可能已經錯過了一個點或兩個..

一般來說,$('elemID') =>$('#elemID')$F('elemID') =>$('#elemID').val()就是我所做的..

// JavaScript Document 
// calculate price based on quantity 
function changeQty(change){ 
var currentQty = parseInt($('#quant').val()) // Where quant is the id of your quantity input field. Gets value of currentQty field 

switch (change) { 
    case 'add': 
     currentQty += 1 
     $('#quant').val(currentQty) 
     calculate() 
     break 
    case 'subtract': 
     if (currentQty > 1) { // only subtract if qty is greater than zero 
      currentQty -= 1 
      $('#quant').val(currentQty) 
      calculate() 
     } 
     break 
    case 'field': 
     if (currentQty > 0) { 
      window.setTimeout('calculate()', 500) 
     } 
     break 
} 
} 
function calculate(){ 
var currentQty = parseInt($('#quant').val()) // Where quant is the id of your quantity input field. Gets value of currentQty field  
var jsnormalprice = $('#jsnormalprice').val() // Where jsnormalprice is the id of your hidden base price field. Gets value of base_price field  
var jsspecialprice = $('#jsspecialprice').val() // Where is the id of your hidden base price field. Gets value of base_price field 

if (currentQty > 0) { // Don't want price to display if zero if customer zeros out quantity 
    var new_jsnormalprice = jsnormalprice * currentQty // Calculate the price.  
    var new_jsnormalprice = new_jsnormalprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down. 

    var new_jsspecialprice = jsspecialprice * currentQty // Calculate the price.   
    var new_jsspecialprice = new_jsspecialprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down. 

} else { // set price back to original price 
    new_jsnormalprice = jsnormalprice 
    new_jsspecialprice = jsspecialprice 
} 

$('#jsnormalpriceshow').html(new_jsnormalprice) // Where jsnormalpriceshow is the id of your span for the echoed product price 
$('#jsspecialpriceshow').html(new_jsspecialprice) // Where jsspecialpriceshow is the id of your span for the echoed product price 

} 
+0

謝謝你,可以完美運行。 –

+0

沒問題.. :) –

1

原型沒有沒有衝突模式。

1

不幸的是,原型沒有沒有衝突模式。

幸運的是,您不需要使用Prototype來選擇DOM中的元素。 jQuery對此很好。

而不是

$F('quant') 
$('quant').value = currentQty 
$F('jsnormalprice') 
$('jsnormalpriceshow').update(new_jsnormalprice) 

您可以使用jQuery的等價物:

$("#quant").val() 
$("#quant").val(currentQty) 
$("#jsnormalprice").val() 
$("#jsnormalpriceshow").text(new_jsnormalprice) 

而且,請不要在字符串評估代碼。更改

window.setTimeout('calculate()', 500)  

到這樣做的更自然的方式:

window.setTimeout(calculate, 500)