简体   繁体   English

分配给变量时,jQuery选择器val为“未定义”

[英]jQuery selector val is 'undefined' when assigning to variable

Working on MVC5 app. 在MVC5应用上工作。

I have some hidden inputs on my page.... But, for testing, I changed the type to "text"... 我的页面上有一些隐藏的输入...。但是,为了进行测试,我将类型更改为“文本” ...

<input type="text" id="selectedDate" value="">
<input type="text" id="selectedStartDate" value="">
<input type="text" id="selectedEndDate" value="">

All 3 inputs have values. 所有3个输入都有值。 I can see them. 我可以看到他们。 But, in my javaScript I'm encountering weird behavior. 但是,在我的javaScript中,我遇到了奇怪的行为。 For example.... 例如....

alert($("#selectedStartDate").val());

The above statement displays an alert with the proper value. 上面的语句显示具有适当值的警报。 BUT, if I try to assign this to a variable, it's undefined.... 但是,如果我尝试将其分配给变量,则它是未定义的...。

var startDate = $("selectedStartDate").val();

startDate is undefined. startDate未定义。 Any idea why? 知道为什么吗? These 2 lines are literally adjacent in the code. 这两行在代码中实际上是相邻的。

Thanks! 谢谢!

You are missing the # in 您缺少#号

var startDate = $("selectedStartDate").val();

Should be 应该

var startDate = $("#selectedStartDate").val();

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

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