Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript错误中编辑html输入元素_Javascript_Html - Fatal编程技术网

从javascript错误中编辑html输入元素

从javascript错误中编辑html输入元素,javascript,html,Javascript,Html,我在html中有这个输入字段: <input id="title" type="text" class="" /> 如果用户想要更改my函数自动指定的值(也称为random_name),他只需在输入字段中键入其他内容即可 到目前为止一切正常,但是如果用户改变主意并再次单击“随机”按钮,则会调用该函数并修改“值”属性,但用户仍会看到最后键入的内容,而不是新的随机值 有没有办法解决这个问题或解决办法?只需执行title.value=random\u name 您可以通过element.

我在html中有这个输入字段:

<input id="title" type="text" class="" />
如果用户想要更改my函数自动指定的值(也称为random_name),他只需在输入字段中键入其他内容即可

到目前为止一切正常,但是如果用户改变主意并再次单击“随机”按钮,则会调用该函数并修改“值”属性,但用户仍会看到最后键入的内容,而不是新的随机值


有没有办法解决这个问题或解决办法?

只需执行
title.value=random\u name

您可以通过
element.value=“所需的值”
设置输入值。如果你使用它,它会起作用


函数值(){
var title=document.getElementById(“title”);
title.value=Math.random();//在此处为title.value指定random_名称
}

如果是你的
random\u name
bugging out,你应该发布代码。先试试这个。只需将
Math.random()
替换为
random\u name

,您需要使用
title.value=random\u name而不是
title.setAttribute(“值”,随机名称)


Fiddle:

而不是
title.removeAttribute()
title.setAttribute()
直接分配给
title.value
var title = document.getElementById("title");
title.removeAttribute("value");
title.setAttribute("value",random_name);
<input id="title" type="text" class="" />
<input type="button" class="" onclick="randomValue()" value="Random" />

function randomValue() {
  var title = document.getElementById("title");
  title.value = Math.random(); // assign random_name to title.value here
}