2013-04-02 37 views
-1

我有一个表单字段x_amount,由一个静态数字填充,基于从下拉列表中选择的内容,由于某种原因,我以x_ship_to_address的形式出现。如果选择1或2,则x_amount用25或45填充。如果选择3或4,用户将值输入到payment_mount中,然后x_amount变为payment_amount x 1.03。我想,如果用户选择1或2,那么这两个支付金额和x_amount被填充了25或45.这是工作来填充x_amount与静态数量的JS:用相同的数据填充2个字段,使用Javascript

function SI_money(amount) { 
    // makes sure that there is a 0 in the ones column when appropriate 
    // and rounds for to account for poor Netscape behaviors 
    amount=(Math.round(amount*100))/100; 
    return (amount==Math.floor(amount))?amount+'.00':((amount*10==Math.floor(amount*10))?amount+'0':amount); 
} 

function calcTotal(){ 
var total = document.getElementById('x_amount'); 
var amount = document.getElementById('payment_amount'); 
var payment = document.getElementById('x_ship_to_address'); 

if(payment.selectedIndex == 0) 
    total.value = 'select Type of Payment from dropdown'; 
else if(payment.selectedIndex == 3 || payment.selectedIndex == 4) 
    total.value = SI_money(parseFloat(amount.value * 1.03)); 
else if(payment.selectedIndex == 1) 
    total.value && amount.value = SI_money(25.00); 
else if(payment.selectedIndex == 2) 
    total.value = SI_money(45.00); 
} 

我觉得我想要的calcTotal的最后两个IFS是这样的:

else if(payment.selectedIndex == 1) 
    total.value && amount.value = SI_money(25.00); 
else if(payment.selectedIndex == 2) 
    total.value && amount.value = SI_money(45.00); 

但加入& &抛出错误。我想我只是缺少一些有关语法的东西 - 我怎么说这两个字段都填充了正确的静态数字?

回答

1

&&并不意味着“做这个和那个”。您需要单独执行这些:

total.value && amount.value = SI_money(25.00); <-- wrong 

正确:

total.value = SI_money(25.00); 
amount.value = SI_money(25.00); 

而且你真的需要阅读:Code Conventions for the JavaScript Programming Language。你的代码中有一些可疑的花括号。

+0

好 - 这是一个简单的修复。感谢您的链接,我在每个陈述中添加了大括号。 – Lauren

+0

@Diodeus total.value = amount.value = SI_money(25.00); ?在这里有任何类似不好的练习(虽然我从来没有这样写过) – hop

+0

你可以做到这一点,但你不能沿着范围来确定变量的范围,所以我通常会避免使用id。参见:http://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript –

相关问题