【问题标题】:Check checkbox depending of price in span根据跨度价格选中复选框
【发布时间】:2017-09-10 04:13:46
【问题描述】:

所以我有一个捐赠表格,人们可以选择不同的捐赠金额,所以如果捐赠金额大于或等于 20,我想自动选中一个复选框。

这个html(数据总量不变文本是变化的元素)

<span class="give-final-total-amount" data-total="20.00">$455.00</span>
<input type="checkbox" value="2" name="group[12581][2]" id="mce-group[12581]-12581-1">

这就是我正在尝试使用 jQuery 的方法

$( document ).ready(function() {
if ($(".give-final-total-amount").text() >= 20.00)  {
jQuery("#mce-group[12581]-12581-1").prop("checked", true);
}
else {
    $("#mce-group[12581]-12581-1").prop("checked", false);
}
});

【问题讨论】:

  • 然后,结果如何?你到底有什么问题?
  • 人们在哪里输入捐款金额?
  • 到底是什么问题?

标签: javascript jquery forms checkbox input


【解决方案1】:

美元符号使您无法进行比较。请参阅下面的其他 cmets:

$(function() {
  // Get a reference to the checkbox
  var chk = document.getElementById("mce-group[12581]-12581-1");
  
  // You can't compare the value of the span against a number if the value is not-numeric
  // you have to remove the dollar sign first
  if ($(".give-final-total-amount").text().replace("$", "") >= 20)  {
    // No need for JQuery on this, just set the checked property to true
    chk.checked = true;
  } else {
    // Set checked property to false
    chk.checked = false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="give-final-total-amount" data-total="20.00">$455.00</span>
<input type="checkbox" value="2" name="group[12581][2]" id="mce-group[12581]-12581-1">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2016-06-18
    • 2017-01-28
    相关资源
    最近更新 更多