JavaScript字符串连接不起作用

Jar*_*ngh 3 html javascript

这个连接的JavaScript代码不起作用.我在关闭脚本标签之前尝试了一个警告,它显示但下面的代码我想在第三个或不同的文本字段中显示结果.

HTML:

<input type="text" id="field"><br>
<input type="text" id="str2"><br>
<input type="text" id="str3"><br>
<button onclick="concate()">Concatenate</button>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

var s=document.getElementById("field").value;
var t=document.getElementById("str2").value;
var st=document.getElementById("str3").value;

function concate()
{
    st=s+t;
    document.getElementById("str3").value.innerHTML=st;
    console.log(st);
    document.write(st); 
}
Run Code Online (Sandbox Code Playgroud)

Zak*_*rki 5

.value.innerHTML应该没有功能:

document.getElementById("str3").value = st;
Run Code Online (Sandbox Code Playgroud)

您还应该在函数内部获取字段值并使用关闭函数定义},请查看下面的示例.

希望这可以帮助.


function concate()
{
     var s=document.getElementById("field").value;
     var t=document.getElementById("str2").value;

     document.getElementById("str3").value=s+t;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" id="field"><br>
<input type="text" id="str2"><br>
<input type="text" id="str3"><br>
<button onclick="concate()">Concatenate</button>
Run Code Online (Sandbox Code Playgroud)