简体   繁体   English

文本框值未使用JavaScript更改

[英]Textbox value not changing using javascript

These are different tabs on my page, clicking one of the tabs takes to that section of the page. 这些是我页面上的不同选项卡,单击其中一个选项卡会转到页面的该部分。

<nav>
    <ul>
        <li class="tab-current"><a style="color:red" href="#section-1" class="icon-shop"><span>Home</span></a></li>
        <li><a href="#section-2" class="icon-cup" id="events"><span>Events</span></a></li>
        <li><a href="#section-3" class="icon-food" id="finance"><span>Finance</span></a></li>
        <li><a href="#section-4" class="icon-lab" id="merchandise"><span>Merchandise</span></a></li>
        <li><a href="#section-5" class="icon-truck" id="tasks"><span>Tasks</span></a></li>
        <li><a href="#section-6" class="icon-truck" id="profile"><span>Profile</span></a></li>
    </ul>
</nav>

<section id="section-2" onclick="setTabIndex(2)">
    <!-- content here -->
</section>

<asp:TextBox ID="tabu" runat="server"></asp:TextBox>
function setTabIndex(tab) {
    //store the tab number in a hidden field so you can access it in code behind
    document.getElementById("<% = HiddenField1.ClientID %>").value = tab;
    document.getElementById('tabu').value = tab;
}

Textbox to check if value is being set to the textbox on changing the tab. 用于检查在更改选项卡时是否将值设置为文本框的文本框。 So document.getElementById('tabu').value = tab; 因此document.getElementById('tabu').value = tab; should change the textbox value each time a tab is clicked, right? 应该在每次单击选项卡时更改文本框的值,对吗? But its not shown in textbox. 但是它没有显示在文本框中。

What am I doing wrong? 我究竟做错了什么? Kindly help out. 请帮忙。 Thanks 谢谢

As you're using a runat="server" Web Forms control, ASP.Net will (annoyingly) change the client-side id at runtime. 当您使用runat="server" Web窗体控件时,ASP.Net将(烦人地)在运行时更改客户端id This is why the previous line of your JS code uses the ClientID property of the server control - which is the same technique you need. 这就是为什么JS代码的前一行使用服务器控件的ClientID属性的原因-这是您需要的相同技术。 Try this: 尝试这个:

function setTabIndex(tab) {
    document.getElementById("<%= HiddenField1.ClientID %>").value = tab;
    document.getElementById("<%= tabu.ClientID %>").value = tab;
}

I think the client ID from your server controls are also different. 我认为您的服务器控件中的客户端ID也有所不同。

You can see the exact ID of your textbox using developer tool in browser. 您可以使用浏览器中的开发人员工具查看文本框的确切ID。

Try using, 尝试使用

document.getElementById('tabu').value = tab; in Javascript and 在Javascript和

in ASP.NET 在ASP.NET中

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

same for your hidden field as it's a server side control. 与您的隐藏字段相同,因为它是服务器端控件。

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

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