2017-07-29 71 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> 
相关问题