输入类型日期,jQuery val() 返回元素标签而不是值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18445008/
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
input type date, jQuery val() returns element tag and not value
提问by Jakub Zak
I have a simple input tag:
我有一个简单的输入标签:
<input type="date" class="basic_txt" name="date_birth" id="npdl_dBirth" value=""/>
And I need to assign the value of this input to a variable:
我需要将此输入的值分配给一个变量:
ndpl_dBirth = $('#npdl_dBirth').val();
For some reason this is returning whole tag in a console when I try to print the variable out:
出于某种原因,当我尝试打印变量时,这会在控制台中返回整个标签:
console.log(npdl_dBirth);
is returning this:
正在返回:
<input type="date" name="date_birth" id="r_date_birth" value>
I must be missing something really obvious, but can't find out what. Anyone any ideas? Thx in advance.
我一定遗漏了一些非常明显的东西,但找不到什么。有人有什么想法吗?提前谢谢。
Thank you for your help, just found my mistake, simply misspelled the variable. Thx anyway
感谢您的帮助,刚刚发现我的错误,只是拼错了变量。无论如何,谢谢
回答by Musa
Some browsers use the id of elements as global variables, so since npdl_dBirth
is the id of the input it will be set as a global variable referencing that variable. Try using a different variable name.
一些浏览器使用元素的 id 作为全局变量,因此由于npdl_dBirth
是输入的 id,它将被设置为引用该变量的全局变量。尝试使用不同的变量名称。
Also your variable is ndpl_dBirth
but you try to print npdl_dBirth
您的变量也是,ndpl_dBirth
但您尝试打印npdl_dBirth
回答by Rohan Kumar
I think you should change you variable name
, it should be different from your html element id
我认为你应该改变你variable name
,它应该和你的不同html element id
console.log($('#npdl_dBirth').val());
Will work fine in your case
会在你的工作正常 case
or
或者
var nb=$('#npdl_dBirth').val();
console.log(nb);