根据跨度价格选中复选框

时间:2017-04-13 22:03:28

标签: 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);
}
});

1 个答案:

答案 0 :(得分:2)

美元符号阻止您的比较工作。请参阅以下其他评论:

&#13;
&#13;
$(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;
  }
});
&#13;
<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">
&#13;
&#13;
&#13;