2012-02-09 49 views
-2

我這行代碼,將在PHP替代的特殊字符:
需要使用PHP多個str_replace函數和jQuery text.replace

<?php echo str_replace(" +12,00$,"1200",$this->htmlEscape($_image->getLabel())) ?>" /> 

這個工作,因爲我需要,也是jQuery將有包括這個代碼在其他呼叫:

jQuery("#productImg" + optionValueText.replace(' +12,00€','1200')).fadeIn('slow'); 

這將outpout 1200,它會工作好添加的圖像加載跨度ID。 但我會有多個價格,所以我需要自動執行這項工作。

我需要兩個代碼在beggining 和人物+,從價格上刪除空白所以八方通它將顯示價格爲1200,4400,3399等 爲PHP我想我能做到這一點如果我玩一段時間的代碼,但對於jQuery線也?

有人可以提供兩個代碼嗎?

非常感謝您的幫助!

編輯: 完整的PHP代碼可以加載圖像:

<a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'width=999,height=999,left=666,top=333,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;"> 
<span style="margin-top:-212px;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" alt="no image" id="productImg<?php echo str_replace(" +12,00€","1200",$this->htmlEscape($_image->getLabel())) ?>" /></a></span>  

和jQuery的上下拉的變化將控制什麼樣的形象來隱藏和顯示。

jQuery(document).ready(function() { 

// On document ready hide all images first 

jQuery(".product-image2 img").hide(); 
jQuery("#off").hide(); 
jQuery("#productImgDefault").show(); 

jQuery("#<?=$colour_select_id ?>").change(function() { 
    // Hide all images on slect element change action 
    jQuery(".product-image img").hide(); 
    jQuery(".product-image2 img").hide(); 
     jQuery("#productImgDefault").hide(); 
     jQuery(".product-image .mousetrap").hide();       


    // Get the value of selected option 
    var optionValue = jQuery(this).attr('value'); 
    // Just a test to see if you're getting option value 
    // alert(optionValue); 

    // Get the content (aka inner HTML) of selected option 
    var optionValueText = jQuery.trim(jQuery('#<?=$colour_select_id ?> :selected').text()); 

    // Just a test to see if you're getting right selected option inner text 
    // alert(optionValueText); 
// alert('Selected option has value: ' + optionValue + ' and inner text: ' + optionValueText); 

    // Show the image based on selected value 
    // Whatch out, case sensitive... 

    if(optionValueText == "-- choose --") { 
    jQuery(".product-image .mousetrap").show(); 
    jQuery("#on").fadeIn('slow'); 
     jQuery("#off").hide(); 
jQuery("#productImgDefault").fadeIn('slow'); } else { jQuery("#productImg" + optionValueText.replace(' +12,00€','1200')).fadeIn('slow'); 
    jQuery("#off").fadeIn('slow'); 
    jQuery("#on").hide(); 
} 
    }); 

}); 
+2

是JavaScript預計什麼加價一起工作呢?如果您想對答案進行投票表決,請單擊除答案之外的向上指示的三角形(或者單擊向下指向的三角形上的向下投票)。要接受答案(如果答案很有用並回答了您的問題),請點擊提供答案左側的勾號。一旦問題得到解答,請不要***'關閉線程。' Stack Overflow的意義不在於* * * * * *問題,它是爲了幫助所有人解決與*相同的問題。 – 2012-02-09 13:20:07

+0

謝謝Topener和David。 @David你是什麼意思關於標記?當下拉改變時,JS將重新加載一個div內的圖像。如果你需要我可以粘貼所有的JS代碼,但是非常基本。 – spanakorizo 2012-02-09 13:26:39

+0

我的意思是HTML標記。 JavaScript,因此jQuery,在客戶端工作。爲了提供有關JavaScript的幫助,我們需要了解它的預期用途。因此請顯示相關的HTML。是的,請:目前相關的JavaScript也會有用。 – 2012-02-09 13:28:10

回答

1

的Javascript:

optionValueText.replace(/[\+\s\$\,\€]/g,''); 

PHP:

echo preg_replace('/[\+\s\$\,\€]/','', $this->htmlEscape($_image->getLabel())); 
+0

非常感謝!有效! – spanakorizo 2012-02-09 13:59:07

0

PHP:

<?php echo preg_replace('/^ \+?([0-9]+),([0-9]+)[\$€]$/','$1$2',$this->htmlEscape($_image->getLabel())) ?> 

<?php echo str_replace(',','',trim($this->htmlEscape($_image->getLabel()),' +$€')) ?> 

的JavaScript

jQuery("#productImg" + optionValueText.replace(/^ \+?([0-9]+),([0-9]+)[\$€]$/,'$1$2')).fadeIn('slow'); 

附:沒有測試過,可能會出現一些小錯誤

+0

非常感謝你,不幸的是它不工作。在php或jQuery中沒有錯誤,但圖像不會改變,因此創建此圖像的跨度id時會出現問題。不幸的是,我不能確定,因爲圖像不會出現,並且沒有錯誤信息可以提供建議。在這個代碼中,+ 12,00€之前的空格也被刪除了?正如我在我的第一篇文章中所說,如果我使用簡單的str_replace和text.replace它將起作用。所以我需要一些遵循這種模式的東西。謝謝。 – spanakorizo 2012-02-09 13:50:21