2014-09-01 49 views
0
<select data-placeholder="Please select a payment method.." style="width:50%;" class="chosen" onchange="currencyChange(this.value)"> 
    <option value=""></option> 
    <optgroup label="Cash"> 
     <option value="Paypal">Paypal</option> 
     <option value="Bitcoin">Bitcoin</option> 
     <option value="Western Union">Western Union</option> 
     <option value="Delivery">Delivery</option> 
    </optgroup> 
    <optgroup label="Ingame Currency"> 
     <option value="RS3 GP">RS3 GP</option> 
     <option value="OSRS GP">OSRS GP</option> 
    </optgroup> 
</select> 

<script type="text/javascript"> 
var sign = "null"; 
var placement = "null"; 
float budget = "null"; 
function currencyChange(data){ 
    if (data === 'Paypal') { 
     sign = "$"; 
     placement = "before"; 
    } 
    if (data === 'Bitcoin') { 
     sign = "$"; 
     placement = "before"; 
    } 
    if (data === 'Western Union') { 
     sign = "$"; 
     placement = "before"; 
    } 
    if (data === 'Delivery') { 
     sign = "$"; 
     placement = "before"; 
    } 
    if (data === 'RS3 GP') { 
     sign = "M/GP"; 
     placement = "after"; 
    } 
    if (data === 'OSRS GP') { 
     sign = "M/GP"; 
     placement = "after"; 
    } 

    if (placement === 'before') {                
     document.getElementById("budget").value = sign + document.getElementById("budget").value; 
    } 

    if (placement === 'after') {                
     document.getElementById("budget").value = document.getElementById("budget").value + sign;                
    } 
} 
</script> 

我正在嘗試更改另一個文本字段的貨幣類型id="budget",以便根據付款方式的選擇添加貨幣符號。更改文本字段的貨幣類型

但是在選擇付款方式時我沒有收到這樣的動作。

回答

2

刪除此,它的作品

float budget = "null"; 

而且,

下面添加選擇

<input type="text" id="budget"> 

編輯

 if (placement === 'before') {     
      toreplaceaft=document.getElementById("budget").value; 
      toreplacebef=toreplaceaft.replace("$",""); 
      toreplace=toreplacebef.replace("M/GP",""); 
      document.getElementById("budget").value = sign + toreplace.replace("$",""); 
     } 

     if (placement === 'after') {  
      toreplaceaft=document.getElementById("budget").value; 
      toreplacebef=toreplaceaft.replace("$",""); 
      toreplace=toreplacebef.replace("M/GP",""); 
      document.getElementById("budget").value = toreplace+ sign;                
     } 
+0

並[小提琴](HTTP ://的jsfiddle。 net/pee7d6qr /)。 – Regent 2014-09-01 12:53:07

+0

好的,語法無效的結構導致函數執行被中止(正如在瀏覽器的控制檯日誌中可以看到的那樣)。該代碼在功能上仍然不正確,因爲更改下拉列表中的選擇會導致添加貨幣符號/縮寫詞,而不會先刪除舊符號/縮寫詞。所以你會得到$$$ 42M/GP。 – 2014-09-01 12:56:06

+0

編輯我的帖子,好心的看看它 – 2014-09-01 13:06:39

0

現在,你需要剝去那些「$,男/ GP「的東西從字符串

有了一點幫助從stackoverflow

value = document.getElementById("budget").value; 
value = value.replace(/\D/g,''); //strip non-numeric characters from string 

然後

if (placement === 'before') {                
    document.getElementById("budget").value = sign +" "+ value; 
} 

if (placement === 'after') {                
    document.getElementById("budget").value = value +" "+ sign;                
} 

jsFiddle

1

試試這個撥弄它不會接受雙重標誌。

http://jsfiddle.net/pee7d6qr/1/

if (placement === 'before') { 
    if(document.getElementById("budget").value.indexOf("$") == -1){ 
     document.getElementById("budget").value = sign + document.getElementById("budget").value; 
    } 
    else { 
     document.getElementById("budget").value = document.getElementById("budget").value; 
    } 
} 

if (placement === 'after') {  
    if(document.getElementById("budget").value.indexOf("M/GP") == -1){ 
     document.getElementById("budget").value = document.getElementById("budget").value + sign;  
    } 
    else { 
     document.getElementById("budget").value = document.getElementById("budget").value;  
    } 
} 
0

刪除以JavaScript

預算輸入元素缺少

<input type="text" name="budget" id="budget" value=> 

使用以下代碼浮子可變

if (placement === 'before') {                
    document.getElementById("budget").value = sign + document.getElementById("budget").value.replace(/[^0-9.]/g,''); 
} 

if (placement === 'after') {                
    document.getElementById("budget").value = document.getElementById("budget").value.replace(/[^0-9.]/g,'') + sign;                
}