Html 如何在 ASP.NET webform 上使用 javascript 设置文本框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15440541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:30:27  来源:igfitidea点击:

How to set the value of a text box with javascript on an ASP.NET webform

javascriptasp.nethtml

提问by Firoso

so in a form I have the following control:

所以在一个表单中,我有以下控制:

<asp:TextBox runat="server" ID="monthlyAmount" ClientIDMode="Static"/>

The ClientIDMode Static is because a master page is in use.

ClientIDMode 静态是因为母版页正在使用中。

I then have this Button:

然后我有这个按钮:

<input type="button" id="calculate" onclick="AutoFillEstimate()" value="Calculate Estimate" />

Wired to this Script:

连线到这个脚本:

<script type="text/javascript">
    function AutoFillEstimate() {
        document.getElementById("monthlyAmount").nodeValue = "test";
    }
</script>

I feel like I'm just using nodeValue instead of what I should be using, but I have no idea where to look for reference on these things.

我觉得我只是在使用 nodeValue 而不是我应该使用的,但我不知道在哪里寻找这些东西的参考。

回答by jiiri

If I understand you correctly, you are just trying to set the value, just use:

如果我理解正确,您只是想设置值,只需使用:

document.getElementById("monthlyAmount").value = "test";

回答by MetalPhoenix

The correct way to do this is:

正确的做法是:

document.getElementById("<%= monthlyAmount.ClientID %>").value = "test";