从Javascript函数返回值设置HTML文本框值-(Javascript)

时间:2019-02-21 00:13:15

标签: javascript

如何从javascript返回值设置文本框值?我尝试了以下代码,但无法正常工作-我在哪里错了?

function getname(name) {
  var subindex = name.indexOf(" ") + 1;
  return (name.substring(subindex));

}
var res = (getname("Harpreet Kaur"));
document.getElementById("lname").value = res;
//alert(res);
<body>
  <input type="text" id="lname">
</body>

1 个答案:

答案 0 :(得分:0)

它运作完美:

function getname(name) {
  var subindex = name.indexOf(" ") + 1;
  return (name.substring(subindex));

}
var res = getname("Harpreet Kaur");
document.getElementById("lname").value = res;
<input type="text" id="lname">