[英]Getting the value of an input element in jquery
I would like to get the value of a input using a the a chain class as the identifier in jquery. 我想使用a链类作为jquery中的标识符来获取输入的值。 is this possible?
这可能吗?
I have the following code 我有以下代码
<input class="donate-block__value monthly" type="text" id="donation-amount" name="DonationAmount" value="200" />
and i have tried the following which has resulted in undefined 我尝试了以下导致未定义的结果
var monthlyDonation = $('.donate-block__value .monthly').val();
var monthlyDonation = $('donate-block__value monthly').val();
console.log(monthlyDonation);
I need to target the class Can this be done please? 我需要针对全班学生能做得到吗?
Don't add space between your classes or jquery will start to search within the first class looking for a child element. 不要在类之间添加空间,否则jquery将开始在第一个类中搜索以寻找子元素。 use it like this:
像这样使用它:
var monthlyDonation = $('.donate-block__value.monthly').val();
如果您有多个定义了该类的元素,也可以仅将input
元素作为目标:
$('input.donate-block__value.monthly').val();
Your selector .donate-block__value .monthly
is referring to an element within an element. 您的选择器
.donate-block__value .monthly
引用元素中的元素。 Try just .donate-block__value
or .monthly
仅尝试
.donate-block__value
或.monthly
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.