2017-02-26 138 views
2

我正在嘗試構建一個應用程序,它將提醒用戶從年輕,平均和年齡的全名和年齡類別,但無論我輸入哪個年齡都會顯示「您屬於年輕類別」 。請告訴我我的代碼有什麼問題。那就是:變量和IF/ELSE語句

<script type="text/javascript"> 
    var godine = 25; 
    var starost; 
    if (godine < 30) { 
     starost = " Vi pripadate u kategoriju mladih."; 
    } 
    if ((godine >= 30) && (godine < 70)) { 
     starost = " Vi pripadate u kategoriju srednje starih."; 
    } 
    if (godine > 70) { 
     starost = " Vi pripadate u kategoriju starih"; 
    } 

    </script> 
    <h1>Dobro dosli na kategorisanje starosti</h1> 
    <form action="" name="frmLogin" onsubmit="alert('Korisnice: ' + ' ' + document.frmLogin.txtIme.value + ' ' + document.frmLogin.txtPrezime.value + document.frmLogin.txtStarost.value + starost)"> 
     Korisnicko ime: <input type="text" name="txtIme"/> 
     <br> 
     Korisnicko prezime: <input type="text" name="txtPrezime"> 
     <br> 
     Godine: <input type="text" name="txtStarost"> 
     <br> 
     <input type="submit" value="Pokreni program"> 
    </form> 
+1

因爲你設置了'godine = 25'。 「如果(godine <30)'總是得到滿足。 –

+0

是的,但如果我把75它仍然是一樣的。我也嘗試了一個空變量。 – user3638147

+0

還要注意,你可能應該在這裏使用'if else'。即使它與當前的邏輯無關,如果有人重構它可能會導致錯誤。 –

回答

3

你被默認設置godine值25和只有一次時加載的頁面。

你必須趕上godine輸入與document.getElementById它的價值與每個函數調用。

該功能與button「Pokreni程序」捆綁在一起。每按一下按鈕,您將執行該功能。該函數將從godine輸入中加載新值,starost值將被重置,結果將基於其值。

function check() { 
 

 
    var godine = document.getElementById('godine').value; 
 
    var starost; 
 
    
 
    if (godine < 30) { 
 
    starost = "Vi pripadate u kategoriju mladih."; 
 
    } else if (godine >= 30 && godine < 70) { 
 
    starost = "Vi pripadate u kategoriju srednje starih."; 
 
    } else if (godine > 70) { 
 
    starost = "Vi pripadate u kategoriju starih"; 
 
    } 
 
    console.log(starost); 
 
}
<h1>Dobro dosli na kategorisanje starosti</h1> 
 
<form action="" name="frmLogin" onsubmit="alert('Korisnice: ' + ' ' + document.frmLogin.txtIme.value + ' ' + document.frmLogin.txtPrezime.value + document.frmLogin.txtStarost.value + starost)"> 
 
    Korisnicko ime: <input type="text" name="txtIme" /> 
 
    <br> Korisnicko prezime: <input type="text" name="txtPrezime"> 
 
    <br> Godine: <input type="text" name="txtStarost" id='godine'> 
 
    <br> 
 
    <button type="submit" onclick='check()'>Pokreni program</button> 
 
</form>

如果你只使用一個html文件:

<html> 
 
<head></head> 
 
<body> 
 

 
<h1>Dobro dosli na kategorisanje starosti</h1> 
 
<form action="" name="frmLogin" onsubmit="alert('Korisnice: ' + ' ' + document.frmLogin.txtIme.value + ' ' + document.frmLogin.txtPrezime.value + document.frmLogin.txtStarost.value + starost)"> 
 
    Korisnicko ime: <input type="text" name="txtIme" /> 
 
    <br> Korisnicko prezime: <input type="text" name="txtPrezime"> 
 
    <br> Godine: <input type="text" name="txtStarost" id='godine'> 
 
    <br> 
 
    <button type="submit" onclick='check()'>Pokreni program</button> 
 
</form> 
 

 
<script> 
 
function check() { 
 

 
    var godine = document.getElementById('godine').value; 
 
    var starost; 
 
    
 
    if (godine < 30) { 
 
    starost = "Vi pripadate u kategoriju mladih."; 
 
    } else if (godine >= 30 && godine < 70) { 
 
    starost = "Vi pripadate u kategoriju srednje starih."; 
 
    } else if (godine > 70) { 
 
    starost = "Vi pripadate u kategoriju starih"; 
 
    } 
 
    console.log(starost); 
 
    
 
} 
 
</script> 
 
</body> 
 
</html>

+0

Tnx爲你的幫助。那樣做了。 – user3638147

+0

沒問題的朋友;) – user3638147

+0

@ user3638147謝謝bratko,祝你有個美好的一天。 –