javascript JQuery val() 返回空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15607386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:24:00  来源:igfitidea点击:

JQuery val() returning empty

javascriptjquery

提问by kaizenCoder

I'm stuck on what seems like a trivial issue and I'm probably gonna kick myself for missing this..Anyway, my issue is I'm failing to get the value from a text field.

我被困在一个看似微不足道的问题上,我可能会因为错过这个而自责……无论如何,我的问题是我无法从文本字段中获取值。

HTML:

HTML:

<form>
        <label for="">Enter Username:</label>
        <input id="usernameText" type="text" size="30" />
        &nbsp;&nbsp;<input type="button" value="Generate" onclick="generateQuery(); return     false;" />
</form>

Javascript:

Javascript:

<script type="text/javascript">

        var username = $("#usernameText").val();

        function generateQuery(){

            alert(username);

        }
</script>

I did the following if (jQuery) {..and made sure JQuery is loaded.

我执行了以下操作if (jQuery) {..并确保加载了 JQuery。

In the alert it displays an empty dialog box.

在警报中,它显示一个空对话框。

If I included the $(document).ready();into my script the function generateQuerydoes not get called. Any idea why..?

如果我将 包含在$(document).ready();我的脚本中,generateQuery则不会调用该函数。知道为什么..?

<script type="text/javascript">

    $(document).ready(function(){
        var username = $("#usernameText").val();

        function generateQuery(){

            alert(username);

        }
    });     
</script>

回答by ahren

Assign your variable within the function.

在函数内分配变量。

function generateQuery(){
  var username = $("#usernameText").val();
  alert(username);
}

As for your other question, "If I included the $(document).ready(); into my script the function generate does not get called. Any idea why..?"

至于您的另一个问题,“如果我将 $(document).ready(); 包含在我的脚本中,则不会调用函数 generate。知道为什么......?”

This happens because of scope. You wrap generateQueryinside an anonymous function when you add a document.readyhandler, and therefore it's not visible to your button onclick="generateQuery()"code.

发生这种情况是因为范围。generateQuery添加document.ready处理程序时,您将包裹在匿名函数中,因此它对您的button onclick="generateQuery()"代码不可见。

回答by PSR

Here it will call while the page is loading.So whenever the page is loading the text box is empty.Try to write within a function.

这里它会在页面加载时调用。所以每当页面加载时,文本框都是空的。尝试在函数中编写。

function generateQuery(){
  var username = $("#usernameText").val();
  alert(username);
}

When you write in document.readyit will cal;l while the page is loading.If you need the value of username then call explicitly.

当您写入时,document.ready它会在页面加载时调用;如果您需要用户名的值,请显式调用。

回答by judda

The value is being derived the first time through. So when the page is first loaded.

该值是第一次派生的。所以当页面第一次加载时。

<script type="text/javascript">
        function generateQuery(){
            var username = $("#usernameText").val();
            alert(username);

        }
</script>

回答by keshu_vats

Here you are defining the function you have to call the function in order to get it run use this generateQuery();line to call your function.

在这里,您定义了必须调用该函数以使其运行的函数,请使用此generateQuery();行来调用您的函数。

回答by Sangeeta

where are you calling the function????

你在哪里调用函数????

i think you need:

我认为你需要:

<script type="text/javascript">

    $(document).ready(function(){
      generateQuery();


    });     

function generateQuery(){
  var username = $("#usernameText").val();
            alert(username);

        }
</script>