[英]Jquery Checkbox is checked?
I am working on making a slider that has different prices using jQuery and a little rangeslider.js.我正在使用 jQuery 和一个小 rangeslider.js 制作一个具有不同价格的滑块。 I made everything work, but the second two numbers only change when I move the slider, not when I check the checkbox.
我使一切正常,但后两个数字仅在我移动滑块时更改,而不是在我选中复选框时更改。 I am trying to make the numbers change right when I click the checkbox.
我试图在单击复选框时正确更改数字。
$(document).on('input change', '#range-slider', '#voiceover', function() { //Listen to slider changes (input changes)
var v=$(this).val();
var voiceOption = $('#voiceover');//Create a Variable (v), and store the value of the input change (Ex. Image 2 [imageURL])
console.log(v);
$('#sliderStatus').html(videoDuration[v]);
$('#sliderPrice').html('<span>$'+videoSubtotal[v]+'.00</span>');
var totalPrice = parseInt(voiceoverSubtotal[v])+parseInt(videoSubtotal[v]);
if(voiceOption.is(":checked")){
$('#voiceoverspan').html('<span>$'+voiceoverSubtotal[v]+'.00</span>');
$('#totalspan').html('<span>$'+totalPrice+'.00</span>');
}
else{
$('#voiceoverspan').html('<span>$0.00</span>');
$('#totalspan').html('<span>$'+videoSubtotal[v]+'.00</span>');
}
/*if(voiceOption.is(":checked") ){
$('#totalspan').html('<span>$'+totalPrice+'.00</span>');
}
else{
$('#totalspan').html('<span>$'+videoSubtotal[v]+'.00</span>');
}*/
});
Thank you so much my codepen is here: https://codepen.io/2cheeky4you/pen/QqGKpy非常感谢我的 codepen 在这里: https ://codepen.io/2cheeky4you/pen/QqGKpy
Updated CodePen: https://codepen.io/bhansa/pen/aLwGZo更新 CodePen: https ://codepen.io/bhansa/pen/aLwGZo
You should add a change event to your checkbox and update the html data in the if statement.您应该向复选框添加更改事件并更新 if 语句中的 html 数据。 Keep your checkbox change method out of the
input#range-slider
change method.将您的复选框更改方法保留在
input#range-slider
更改方法之外。
v = $("input#range-slider").val();
voiceOption.on("change", function(){
if(voiceOption.is(":checked")){
$('#voiceoverspan').html('<span>$'+voiceoverSubtotal[v]+'.00</span>');
$('#totalspan').html('<span>$'+totalPrice+'.00</span>');
}
else{
$('#voiceoverspan').html('<span>$0.00</span>');
$('#totalspan').html('<span>$'+videoSubtotal[v]+'.00</span>');
}
});
您需要对复选框上的更改事件做出反应:
$("#voiceover").on("change", doUpdate);
The problem is that all of your code is wrapped in the change event for the slider.问题是您的所有代码都包含在滑块的更改事件中。
$(document).on('input change', '#range-slider', '#voiceover', function() { //Listen to slider changes (input changes)
That means the check for the checkbox changing only happens when the slider changes too.这意味着检查复选框更改仅在滑块更改时才会发生。 Move this line
移动这条线
});
ABOVE your commented code.在您注释的代码之上。 Then format the commented code at the bottom like this.
然后像这样格式化底部的注释代码。
$('#checkBoxId').on('click', function() {
if(this.checked) {
if(voiceOption.is(":checked") ){
$('#totalspan').html('<span>$'+totalPrice+'.00</span>');
}
else {
$('#totalspan').html('<span>$'+videoSubtotal[v]+'.00</span>');
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.