所以我对javascript很新,我想知道为什么当单击按钮时输出不显示“Hello John,你的姓氏是Johnsen”。我用不同的代码尝试了几次,它似乎没有用。我在控制台中也出现错误,其中显示“Uncaught TypeError:name不是函数 在HTMLButtonElement.onclick“
代码和输出图片:(我无法在代码框中对不起)
代码:
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>
答案 0 :(得分:2)
<br>
需要包含在"
。
答案 1 :(得分:0)
检查一下。
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;
答案 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>