Javascript document.getElementById(';TextBox1';)。值不起作用

Javascript document.getElementById(';TextBox1';)。值不起作用,javascript,c#,asp.net,Javascript,C#,Asp.net,我想用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

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

var=false;var类型;
window.onbeforeunload=函数(){
如果(!已提交){
return“是否确定要离开此页面,您的测试将不会保存?”+键入;
}
}; 
document.getElementById('form1')。onsubmit=function(){
类型=document.getElementById('TextBox1')。值;
提交=真实;
}; 
无论我键入什么,它都不会给我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>
var=false;var类型; $(文档).ready(函数() { $('#form1')。提交(函数() { 类型=document.getElementById('TextBox1')。值; 提交=真实; }); window.onbeforeunload=函数() { 如果(!已提交) { return“是否确定要离开此页面,您的测试将不会保存?”+键入; } }; });
如果要在警报框中显示类型的值,请将if(!submitted')更改为if(submitted),您将收到类型值为的警报。希望这样做。

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


当您运行
onsubmit
时,它将
submitted
设置为true,然后在卸载之前运行
onbeforeunload
。现在它基本上只在您没有运行submit时返回输入值。如果您选择
(!submitted)
中,它将向您发出带有输入值的警报。

文本框在哪里?如果您的代码位于HTML源中的元素之前,它将无法工作。将
移动到主体的末端。@Pointy What??问题在提交处理程序中。虽然如果代码在表单之前,那么……在代码中,您将
TextBox1
作为
id
,在文本中有
TextBox1
gEBI()
中的id错误?您是否查看了控制台中的错误?发布您的完整代码OP在哪里提到了jQuery的使用?(提示,它们不会……如果您要在答案中使用jQuery,那么您需要强调它们需要包含jQuery库。)