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
I think you need the server tag when you get the textbox and hidden field because they're run at server by 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" />
PayDate is propably just empty or 'undefined'
if (PayDate == '' || PayDate == 'undefined') {
// Retreive the next field in the tab sequence, and give it the focus.
document.getElementById('txtPaymentDate').value = hdn1.value;
}
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.
<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>
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 == ''
精彩评论