复选框触发输入,反之亦然
Checkbox to trigger input, and vice versa
我目前正在处理表格,需要执行以下操作:
如果选中输入字段(在本例中为数量字段)旁边的复选框,则将输入设置为“1”。如果数量字段大于一,则复选框保持选中状态。
如果复选框未选中,则数量字段重置为 0。或者,如果用户在数量字段中输入 0,则复选框也未选中
问题
- 增加按钮目前需要点击两次才能增加数量
- 当数量字段中有一个数字>0,然后减少到0时,增加按钮不再起作用。
任何人都可以发现我的方法中的错误吗?
jQuery('.exhibitorBooking--table .desc input[type="checkbox"]').on('change', function(){
if(this.checked){
jQuery(this).parents('tr').find('.qty-field input[type="text"]').val('1');
} else {
jQuery(this).parents('tr').find('.qty-field input[type="text"]').val('0');
}
});
jQuery('.exhibitorBooking--table .qty-field input[type="text"]').on('change', function(){
if(jQuery(this).val() == 0){
jQuery(this).parents('tr').find('input[type="checkbox"]').attr('checked', false);
} else if(jQuery(this).val() == 1) {
jQuery(this).parents('tr').find('input[type="checkbox"]').trigger('change').attr('checked', true);
} else {
jQuery(this).parents('tr').find('input[type="checkbox"]').attr('checked', true);
}
});
jQuery(document).ready(function() {
$('#chkStand-1-1').on('change', function() {
var value = $(this).is(':checked') ? 1 : 0;
$('.input-text').val(value);
});
$('.input-text').on('keyup', function() {
$('#chkStand-1-1').prop('checked', (parseInt($(this).val(), 10) > 0));
});
});
我目前正在处理表格,需要执行以下操作:
如果选中输入字段(在本例中为数量字段)旁边的复选框,则将输入设置为“1”。如果数量字段大于一,则复选框保持选中状态。
如果复选框未选中,则数量字段重置为 0。或者,如果用户在数量字段中输入 0,则复选框也未选中
问题
- 增加按钮目前需要点击两次才能增加数量
- 当数量字段中有一个数字>0,然后减少到0时,增加按钮不再起作用。
任何人都可以发现我的方法中的错误吗?
jQuery('.exhibitorBooking--table .desc input[type="checkbox"]').on('change', function(){
if(this.checked){
jQuery(this).parents('tr').find('.qty-field input[type="text"]').val('1');
} else {
jQuery(this).parents('tr').find('.qty-field input[type="text"]').val('0');
}
});
jQuery('.exhibitorBooking--table .qty-field input[type="text"]').on('change', function(){
if(jQuery(this).val() == 0){
jQuery(this).parents('tr').find('input[type="checkbox"]').attr('checked', false);
} else if(jQuery(this).val() == 1) {
jQuery(this).parents('tr').find('input[type="checkbox"]').trigger('change').attr('checked', true);
} else {
jQuery(this).parents('tr').find('input[type="checkbox"]').attr('checked', true);
}
});
jQuery(document).ready(function() {
$('#chkStand-1-1').on('change', function() {
var value = $(this).is(':checked') ? 1 : 0;
$('.input-text').val(value);
});
$('.input-text').on('keyup', function() {
$('#chkStand-1-1').prop('checked', (parseInt($(this).val(), 10) > 0));
});
});