javascript - How to get the textbox value from a hidden field value assigned - Stack Overflow

I have written the following script to get the value of hidden field to a text box when text box is emp

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 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

Share Improve this question asked Jul 25, 2011 at 12:49 VivekhVivekh 4,25912 gold badges62 silver badges105 bronze badges 5
  • Probably PayDate is not null but undefined or an emtpy string? ON the other hand it should read onChange="Tab" without the parenthesis and the semicolon. – Sebastian P.R. Gingter Commented Jul 25, 2011 at 12:52
  • Even if i write != issue remains same – Vivekh Commented Jul 25, 2011 at 12:53
  • @Sebastian Didn't Work – Vivekh Commented Jul 25, 2011 at 12:54
  • are you inside a content page ( master page )? what is your asp version? – codeandcloud Commented Jul 25, 2011 at 12:59
  • Naveen 3.5 i am using 2008 – Vivekh Commented Jul 25, 2011 at 13:01
Add a ment  | 

5 Answers 5

Reset to default 4

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 == ''

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742299621a4417792.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信