繁体   English   中英

document.getElementById('TextBox1')。value不起作用

[英]document.getElementById('TextBox1').value is not working

我想在javascript中获取文本框的值。 这是我的javascript-

<script type="text/javascript">
    var submitted = false;  var type;
    window.onbeforeunload = function () { 
        if (!submitted) { 
            return "Are you sure you want to leave this page, your test will not be saved?"+type; 
        }
    }; 

    document.getElementById('form1').onsubmit = function () { 
        type=document.getElementById('TextBox1').value; 
        submitted = true;
    }; 
</script>

不管我键入什么,都不会给我textbox1的值

 <script type="text/javascript">
        var submitted = false; var type;
        $(document).ready(function ()
        {
            $('#form1').submit(function ()
            {
                type = document.getElementById('TextBox1').value;
                submitted = true;
            });
            window.onbeforeunload = function ()
            {
                if (!submitted)
                {
                    return "Are you sure you want to leave this page, your test will not be saved?" + type;
                }
            };
        });



    </script>

如果要在警报框中显示类型的值,则将if(!submitted')更改为if(submitted),您将收到一个类型为value的警报。希望这样做。

这看起来像是一个逻辑问题:

当您运行onsubmit ,它设置submitted到真实的,然后它运行onbeforeunload 现在,它基本上仅在未运行onsubmit时才返回输入值。 如果拿! 超出(!submitted) ,它将为您提供输入值的警报。

暂无
暂无

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

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