javascript - 根据范围内的价格选中复选框

标签 javascript jquery forms checkbox input

所以我有一个捐赠表格,人们可以选择不同的捐赠金额,所以我想在捐赠金额大于或等于 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);
}
});

最佳答案

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

$(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">

关于javascript - 根据范围内的价格选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43402435/

相关文章:

javascript - 当选项卡为空时隐藏选项卡

php - 在编辑时用数据库值填充表单字段

javascript - Bootstrap如何实现左右缩进的定顶菜单?

javascript - AngularJS 获得具有特定值的重复项中的第一项

javascript - Jquery 在悬停时淡化背景?

jQuery:排除列表树的根元素(ul)

javascript - 如何使用 JavaScript 和 HTML 开发复选框表单?

forms - 显示另一个表单时防止出现主表单

javascript - 将 slideToggle 框对齐到父 div 的底部

php - 将变量从 PHP 传递到 javascript 并传递到 html 表单