javascript 如何从分配的隐藏字段值中获取文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6816362/
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
How to get the textbox value from a hidden field value assigned
提问by Vivekh
I have written the following script to get the value of hidden field to a text box when text box is empty on hitting tab but it did not works so can any one tell what's wrong in this
我编写了以下脚本,当点击选项卡上的文本框为空时,将隐藏字段的值获取到文本框,但它不起作用,所以任何人都可以说出这有什么问题
<script type="text/javascript">
function Tab() {
var PayDate = document.getElementById('txtDate').value;
var hdn1 = document.getElementById('hdn1');
if (PayDate == null) {
// Retreive the next field in the tab sequence, and give it the focus.
document.getElementById('txtDate').value = hdn1.value;
}
}
</script>
<asp:HiddenField ID="hdn1" runat="server" />
<asp:TextBox ID="txtDate" runat="server" onChange="Tab();"></asp:TextBox>
<asp:Button ID="btn" runat="server" Text="Button" />
On my page load i write this
在我的页面加载我写这个
if (!IsPostBack)
{
hdn1.Value = "1-2-2001";
}
But i am not getting the value of hidden field assigned to text box when i am hitting tab can any one help me
但是当我点击选项卡时,我没有得到分配给文本框的隐藏字段的值,任何人都可以帮助我
回答by Marwan
I think you need the server tag when you get the textbox and hidden field because they're run at server by ID
我认为当你获得文本框和隐藏字段时你需要服务器标签,因为它们是通过 ID 在服务器上运行的
<script type="text/javascript">
function Tab() {
var PayDate = document.getElementById('<%= txtDate.ClientID %>').value;
var hdn1 = document.getElementById('<%= hdn1.ClientID %>');
if (PayDate == '') {
// Retreive the next field in the tab sequence, and give it the focus.
document.getElementById('<%= txtDate.ClientID %>').value = hdn1.value;
}
}
</script>
<asp:HiddenField ID="hdn1" runat="server" />
<asp:TextBox ID="txtDate" runat="server" onChange="Tab();"></asp:TextBox>
<asp:Button ID="btn" runat="server" Text="Button" />
回答by N0Alias
Since your hidden field has the RunatServer property set to true its real ClientID probably isn't 'hdn1'. If you're using 4.0 you can set the ClientIDMode="Static" so that it always is 'hdn1' or you can use a server tag in the javascript and use the ClientID property, or use a Jquery selector to be able to easily get a reference to the hidden value.
由于您的隐藏字段将 RunatServer 属性设置为 true,因此其真正的 ClientID 可能不是“hdn1”。如果您使用的是 4.0,您可以设置 ClientIDMode="Static" 使其始终为“hdn1”,或者您可以在 javascript 中使用服务器标记并使用 ClientID 属性,或使用 Jquery 选择器来轻松获取对隐藏值的引用。
回答by Developer
<script type="text/javascript"> function Tab() {
var PayDate = document.getElementById('<%= txtDate.ClientID %>').value;
var hdn1 = document.getElementById('<%= hdn1.ClientID %>');
if (PayDate == '') {
// Retreive the next field in the tab sequence, and give it the focus.
document.getElementById('<%= txtDate.ClientID %>').value = hdn1.value;
}
} </script>
<asp:TextBox ID="txtDate" runat="server" onBlur="Tab();"></asp:TextBox> or
<asp:TextBox ID="txtDate" runat="server" onKeyUp="Tab();"></asp:TextBox>
<asp:TextBox ID="txtDate" runat="server" onBlur="Tab();"></asp:TextBox> or
<asp:TextBox ID="txtDate" runat="server" onKeyUp="Tab();"></asp:TextBox>
回答by R?zvan Flavius Panda
Try using the onchange event with a non-capital 'c' and in the if condition the test must be for an empty string like PayDate == ''
尝试使用带有非大写 'c' 的 onchange 事件,并且在 if 条件下,测试必须是空字符串,如 PayDate == ''
回答by Johnny5
PayDate is propably just empty or 'undefined'
PayDate 可能只是空的或“未定义”
if (PayDate == '' || PayDate == 'undefined') {
// Retreive the next field in the tab sequence, and give it the focus.
document.getElementById('txtPaymentDate').value = hdn1.value;
}