2013-04-02 3 views
-1

x_ship_to_address로 나에게 와서 드롭 다운의 선택에 따라 정적 숫자로 채워지는 양식 필드 x_amount가 있습니다. 1 또는 2를 선택하면 x_amount가 25 또는 45로 채워집니다. 3 또는 4를 선택하면 사용자가 payment_mount에 값을 입력 한 다음 x_amount가 payment_amount x 1.03이됩니다.자바 스크립트를 사용하여 두 개의 데이터를 동일한 데이터로 채 웁니다.

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

난 내가 원하는 생각 : 사용자가 다음 PAYMENT_AMOUNT 및 x_amount 모두 25 여기 45 채워 정적 번호 x_amount를 채우는 작업하는 JS이다되어 하나를 선택하거나 둘 경우 I는 원하는

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

그러나 부가 & &가 오류를 던져 : calcTotal의 마지막 두 IFS 뭔가처럼 될 수 있습니다. 난 그냥 문법에 대해 뭔가를 놓친 것 같아요 - 어떻게 두 필드를 올바른 정적 번호로 채워지라고합니까?

답변

1

&&은 "do this and that"을 의미하지 않습니다. 당신은 별도로 실행해야합니다

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 –

관련 문제