单击按钮时为什么javascript不显示输出

时间:2018-03-31 10:38:12

标签: javascript html css

所以我对javascript很新,我想知道为什么当单击按钮时输出不显示“Hello John,你的姓氏是Johnsen”。我用不同的代码尝试了几次,它似乎没有用。我在控制台中也出现错误,其中显示“Uncaught TypeError:name不是函数     在HTMLButtonElement.onclick“

代码和输出图片:(我无法在代码框中对不起)

Output

代码:

function Name() {
var firstname = document.getElementById("fname").value;
var lastname = document.getElementById("lname").value;

document.getElementById("demo").innerHTML = "Hello " + firstname + <br> + "Your lat name is: " + lastname;
}
<div>

First Name: <input type="text" id="fname" value=""><br><br>
Last Name: <input type="text" id="lname" value="">

<button onclick="Name()">Submit</button>

<p id="demo"></p>

</div>

Console error

3 个答案:

答案 0 :(得分:2)

<br>需要包含在"

答案 1 :(得分:0)

检查一下。

&#13;
&#13;
function clicked() {
var firstname = document.getElementById("fname").value;
var lastname = document.getElementById("lname").value;

document.getElementById("demo").innerHTML = "Hello " + firstname + "<br>" + "Your lat name is: " + lastname;
}
&#13;
<div>

First Name: <input type="text" id="fname" value=""><br><br>
Last Name: <input type="text" id="lname" value="">

<button onclick="clicked()">Submit</button>

<p id="demo"></p>

</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

var fname = document.getElementById('fname');
var lname = document.getElementById('lname');
var res = document.getElementById('demo')
function fullName(){
res.innerHTML = `Firstname :  ${fname.value} <br/> lastname :  ${lname.value}`;
}
<div>

First Name: <input type="text" id="fname" value=""><br><br>
Last Name: <input type="text" id="lname" value="">

<button onclick="fullName()">Submit</button>

<p id="demo"></p>

</div>