更改文本字段货币类型

时间:2014-09-01 12:43:06

标签: javascript php jquery html

<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"的货币类型,以根据付款方式的选择添加货币符号。?

但是在选择付款方式时我没有采取这样的行动。

4 个答案:

答案 0 :(得分: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;                                                            
        }

答案 1 :(得分: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;     
    }
}

答案 2 :(得分:0)

现在你需要从字符串中删除那些“$,M / 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

答案 3 :(得分:0)

在javascript中删除float变量

缺少预算输入元素

<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;                                                            
}