2017-08-22 56 views
0

當我單擊調用showDetails()函數的提交按鈕時,nameemail輸入框顯示爲未定義。當輸入框中有值時,也會發生這種情況:爲什麼我的輸入框顯示未定義?

<script type="text/javascript"> 
function showDetails(){ 

    var e = document.getElementById("email"); 
    //var t = document.getElementById("t"); 
    var n = document.getElementById("name"); 
    var m = document.getElementById("m"); 

    var c; 
    var r; 
    document.getElementById("email").value = c; 
    document.getElementById("name").value = r; 
    document.getElementById("t").value = "Your name is: "+r+" and your email is: "+c; 
} 
</script> 

爲什麼會發生這種情況?

+2

你分別給email'的'和name'值''要和C''r',也沒有任何設置值(你已經得到了他們南轅北轍) – billyonecan

+0

瓦爾'C '和'r'沒有定義,並且將它們分配給元素值。每次調用該函數。 –

+1

是的,這完全是倒退:'document.getElementById(「email」)。value = c'。所有你需要的是'c = e.value' –

回答

3

我相信這是因爲你將值賦給undefined。

<script type="text/javascript"> 
    function showDetails() { 

    var e = document.getElementById("email"); 
    //var t = document.getElementById("t"); 
    var n = document.getElementById("name"); 
    var m = document.getElementById("m"); 

    var c = document.getElementById("email").value; // Here you set it the value = undefined variable 
    var r = document.getElementById("name").value; 

    document.getElementById("t").value = "Your name is: " + r + " and your email is: " + c; 
    } 
</script> 
+1

或'var c = e.value'和'var r = n.value' – billyonecan

+0

@billyonecan絕對! – Riddell

0

我覺得這個任務有問題。嘗試這個。

c=document.getElementById("email").value;r=document.getElementById("name").value;