【问题标题】:Getting the value of an input element in jquery在jquery中获取输入元素的值
【发布时间】:2016-07-18 21:11:24
【问题描述】:

我想使用 a 链类作为 jquery 中的标识符来获取输入的值。这可能吗?

我有以下代码

<input class="donate-block__value monthly" type="text" id="donation-amount" name="DonationAmount" value="200" />

我尝试了以下导致未定义的方法

var monthlyDonation = $('.donate-block__value .monthly').val();
var monthlyDonation = $('donate-block__value monthly').val();

console.log(monthlyDonation);

我需要定位类,请问可以这样做吗?

【问题讨论】:

    标签: javascript jquery class input


    【解决方案1】:

    不要在你的类之间添加空格,否则 jquery 将开始在第一个类中搜索子元素。像这样使用它:

    var monthlyDonation = $('.donate-block__value.monthly').val();
    

    【讨论】:

    • 就是这样:)!我不敢相信我错过了。这是漫长的一天。谢谢
    【解决方案2】:

    如果您有多个定义了该类的元素,您也可以只定位input 元素:

    $('input.donate-block__value.monthly').val();

    【讨论】:

      【解决方案3】:

      您的选择器.donate-block__value .monthly 指的是元素中的元素。试试.donate-block__value.monthly

      【讨论】:

        猜你喜欢
        • 2011-04-10
        • 2012-12-29
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        • 2020-09-12
        • 1970-01-01
        • 1970-01-01
        • 2018-04-20
        相关资源
        最近更新 更多