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

document.getElementById('TextBox1').value is not working

本文关键字:value 不起作用 TextBox1 document getElementById      更新时间:2023-09-26

我想在 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>

无论我键入什么,它都不会给我文本框1的值

 <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(已提交),您将收到类型为 value 的警报。

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

当您运行 onsubmit 时,它会将submitted设置为 true,然后运行onbeforeunload 。现在它基本上只返回输入值,如果你没有运行onsubmit。如果您将!(!submitted)中取出,它将为您提供包含输入值的警报。