简体   繁体   English

输入的值不是由JavaScript设置的(或者是?)

[英]input's value isn't set by JavaScript (or is it?)

I'm trying to pass information from js to codebehind. 我正在尝试将信息从js传递到codebehind。 When setting a breakpoint at the end of the js - The (Firefox) debugger (-js) shows me that an input 's value has been set, while the (Firefox) Inspector (-html) shows it hasn't . 当在JS结束设置断点-在(火狐) 调试器 (-js)让我发现了input的值定,而(火狐) 督察 (-html)表明它没有 When reaching the codebehind - it hasn't been set. 当到达代码隐藏 -它没有被设置。 Why? 为什么?

js to set the value and click a button that will then call the codebehind: js设置值并单击一个按钮,然后将调用其背后的代码:

<script type="text/javascript">
    function doit(s, input, button)
    {
        var i = document.getElementById(input);
        i.innerHTML = s;
        var b = document.getElementById(button);
        b.click();
    }       
</script>

html with hidden input and button (for calling the codebehind) and a button to begin all of this, and a div to show the result: 带有隐藏的输入和按钮(用于调用代码背后的代码)的html和一个用于开始所有这些操作的按钮,以及一个用于显示结果的div:

<asp:Button ID="Button1" runat="server" Text="Test" OnClick="Button1_Click" />
<div style="display: none;">
    <input id="info" runat="server" />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" />
</div>
<div id="result" runat="server">
</div>

C# codebehind to call the js, and to show the result: C#代码隐藏以调用js,并显示结果:

protected void Button1_Click(object sender, EventArgs e)
{
    ClientScriptManager cs = Page.ClientScript;
    Type pageType = GetType();
    cs.RegisterStartupScript(
        GetType(),
        "aName",
        "doit('abc', '" + info.ClientID + "','" + Button2.ClientID + "');", 
        true);
}

protected void Button2_Click(object sender, EventArgs e)
{
    result.InnerText = info.Value;
}

You need to set value property of input instead of innerHTML 您需要设置输入的value属性,而不是innerHTML

Change 更改

i.innerHTML = s;

To

i.value = s;

You want to use value to set the value of an input: 您要使用value来设置输入的值:

function doit(s, input, button) {
    document.getElementById(input).value = s;
    var b = document.getElementById(button);
    b.click();
}       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM