2017-07-29 132 views
0

用於下面的代碼,我必須使用4只起到從4個輸入字段中顯示的值。單功能來捕獲不同值的不同輸入字段

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
Name: <input type="text" id="name1" value="" onBlur="myFunction1()"><br> 
 
Age: <input type="number" id="age1" value="" onBlur="myFunction2()"><br> 
 
Job: <input type="text" id="job1" value="" onBlur="myFunction3()"> <br> 
 
Tel: <input type="text" id="tel1" value="" onBlur="myFunction4()"> <br> 
 

 
<p>Type then click outside to display the value of the input field.</p> 
 

 
Your input: 
 
<p id="demo"></p> 
 

 
<script> 
 
function myFunction1() { 
 
    var x = document.getElementById("name1").value; 
 
    document.getElementById("demo").innerHTML = x; 
 
} 
 
function myFunction2() { 
 
    var y = document.getElementById("age1").value; 
 
    document.getElementById("demo").innerHTML = y; 
 
} 
 
function myFunction3() { 
 
    var z = document.getElementById("job1").value; 
 
    document.getElementById("demo").innerHTML = z; 
 
} 
 
function myFunction4() { 
 
    var a = document.getElementById("tel1").value; 
 
    document.getElementById("demo").innerHTML = a; 
 
} 
 
</script> 
 

 
</body> 
 
</html>

有沒有辦法只能使用1個函數來完成同樣的工作方式?

+1

你知道的參數? –

+0

不是,它做了什麼? –

回答

4

試試這個

function myFunction1(val) { 
 
    var x = val; 
 
    document.getElementById("demo").innerHTML = x; 
 
}
Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.value)"><br> 
 
Age: <input type="number" id="age1" value="" onBlur="myFunction1(this.value)"><br> 
 
Job: <input type="text" id="job1" value="" onBlur="myFunction1(this.value)"> <br> 
 
Tel: <input type="text" id="tel1" value="" onBlur="myFunction1(this.value)"> <br> 
 

 
<p>Type then click outside to display the value of the input field.</p> 
 

 
Your input: 
 
<p id="demo"></p>

+0

有什麼用'變種X = val的;'?你可以直接分配'val'! –

+0

雅,你可以直接使用'val' – Bhargav

+0

我做同樣的,我問你!如果有人可以直接使用,那麼爲什麼你要額外增加一行來編譯? –

2

你可以這樣做:

<!DOCTYPE html> 
<html> 
<body> 

Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.id)"><br> 
Age: <input type="number" id="age1" value="" onBlur="myFunction1(this.id)"><br> 
Job: <input type="text" id="job1" value="" onBlur="myFunction1(this.id)"> <br> 
Tel: <input type="text" id="tel1" value="" onBlur="myFunction1(this.id)"> <br> 

<p>Type then click outside to display the value of the input field.</p> 

Your input: 
<p id="demo"></p> 

<script> 
function myFunction1(ID) { 
    var x = document.getElementById(ID).value; 
    document.getElementById("demo").innerHTML = x; 
} 

</script> 

</body> 
</html> 
0

function myFunction(val) { 
 
    document.getElementById("demo").innerHTML = val; 
 
}
Name: <input type="text" id="name1" value="" onBlur="myFunction(this.value)"><br> 
 
Age: <input type="number" id="age1" value="" onBlur="myFunction(this.value)"><br> 
 
Job: <input type="text" id="job1" value="" onBlur="myFunction(this.value)"> <br> 
 
Tel: <input type="text" id="tel1" value="" onBlur="myFunction(this.value)"> <br> 
 

 
<p>Type then click outside to display the value of the input field.</p> 
 

 
Your input: 
 
<p id="demo"></p>

+0

你給一個已經由3分鐘前給出相同的答案 – Bhargav

0

傳遞參數這個值的功能。

<body> 

Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.value)"><br> 
Age: <input type="number" id="age1" value="" onBlur="myFunction1(this.value)"><br> 


<p>Type then click outside to display the value of the input field.</p> 

Your input: 
<p id="demo"></p> 

<script> 
function myFunction1(val) { 

    document.getElementById("demo").innerHTML = val; 

} 

</script> 

但我認爲你需要的是這一個。

Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.value)"><br> 
Age: <input type="number" id="age1" value="" onBlur="myFunction2(this.value)"><br> 


<p>Type then click outside to display the value of the input field.</p> 

Name Your input: 
<p id="demo"></p> 

Age Your input: 
<p id="demo1"></p> 

<script> 
function myFunction1(val) { 

    document.getElementById("demo").innerHTML = val; 

} 

function myFunction2(val) { 

    document.getElementById("demo1").innerHTML = val; 

} 

</script> 
1
<!DOCTYPE html> 
<html> 
<body> 

Name: <input type="text" id="name1" value="" onBlur="myFunction(this)"><br> 
Age: <input type="number" id="age1" value="" onBlur="myFunction(this)"><br> 
Job: <input type="text" id="job1" value="" onBlur="myFunction(this)"> <br> 
Tel: <input type="text" id="tel1" value="" onBlur="myFunction(this)"> <br> 

<p>Type then click outside to display the value of the input field.</p> 

Your input: 
<p id="demo"></p> 

<script> 
function myFunction(arg) { 

    var id = arg.getAttribute('id'); 
    var x = document.getElementById(id).value; 

    document.getElementById("demo").innerHTML = x; 
} 

</script> 

</body> 
</html> 
相關問題