返回JS函数的值并将其用作输入按钮的值

Jul*_*ian 4 html javascript

我想设置一个返回字符串的JavaScript函数,该字符串用作按钮的值.像这样的东西:

function test(){
 x = "some text";
 return x;
}
Run Code Online (Sandbox Code Playgroud)

我想在value属性的input元素中使用此函数.像这样的东西:

<input type="button" id="s1" value="test()" />
Run Code Online (Sandbox Code Playgroud)

不幸的是,按钮不会在按钮上显示"某些文本",而是"test()".我怎样才能解决这个问题?

Sud*_*oti 8

你可以这样做:

<input type="button" id="s1" value="" />
<script type="text/javascript">
 var elem = document.getElementById("s1");
 elem.value = "some text";
</script>
Run Code Online (Sandbox Code Playgroud)