javascript - jQuery - 复选框不更新文本

标签 javascript jquery

我目前正在根据选中的复选框返回一些文本。我添加了一个复选框来自动检查所有国家/地区,但不会返回任何文本。无法弄清楚我做错了什么。

这是代码和 fiddle :

HTML

<p><label><input type="checkbox" id="checkCountries" /> Check Countries</label></p>

<fieldset>
  <legend>Loads of checkboxes</legend>
  <p><label><input type="checkbox" value="1" /> USA</label></p>
  <p><label><input type="checkbox" value="2" /> Canada</label></p>
  <p><label><input type="checkbox" value="3" /> UK</label></p>
  <p><label><input type="checkbox" value="4" /> Spain</label></p>
  <p><label><input type="checkbox" value="10" /> Male</label></p>
  <p><label><input type="checkbox" value="11" /> Female</label></p>
</fieldset>
<div class="print"></div>
<div class="print2"></div>
<div class="print3"></div>
<div class="print4"></div>
<div class="print10"></div>
<div class="print11"></div>

和 jQuery

$("#checkCountries").change(function() {
  $("input[value='1']").prop('checked', $(this).prop("checked"));
  $("input[value='2']").prop('checked', $(this).prop("checked"));
  $("input[value='3']").prop('checked', $(this).prop("checked"));
  $("input[value='4']").prop('checked', $(this).prop("checked"));
});


$(document).ready(function() {
  $('.print').val($(this).is(':checked'));

  $('input[value="1"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).attr("checked", $('.print').text("USA is Checked"));
    } else {
      $('.print').empty();
    }
    $('.print').val($(this).is(':checked'));
  });
  $('input[value="2"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).attr("checked", $('.print2').text("Canada is Checked"));
    } else {
      $('.print2').empty();
    }
    $('.print2').val($(this).is(':checked'));
  });
  $('input[value="3"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).attr("checked", $('.print3').text("UK is Checked"));
    } else {
      $('.print3').empty();
    }
    $('.print3').val($(this).is(':checked'));
  });
  $('input[value="4"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).attr("checked", $('.print4').text("Spain is Checked"));
    } else {
      $('.print4').empty();
    }
    $('.print4').val($(this).is(':checked'));
  });
  $('input[value="10"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).attr("checked", $('.print10').text("Male is Checked"));
    } else {
      $('.print10').empty();
    }
    $('.print10').val($(this).is(':checked'));
  });
  $('input[value="11"]').change(function() {
    if ($(this).is(":checked")) {
      $(this).attr("checked", $('.print11').text("Female is Checked"));
    } else {
      $('.print11').empty();
    }
    $('.print11').val($(this).is(':checked'));
  });
});

https://jsfiddle.net/gf28v9x5/3/

最佳答案

您需要显式触发更改事件。

请注意,更改事件仅由用户对该特定元素的操作触发。

我做了什么

仅在更改选中/取消选中时触发

$("input[value='1']").prop('checked', $(this).prop("checked")).trigger('change');

喜欢https://jsfiddle.net/4ktbjwL2/1/

$("#checkCountries").change(function () {
    $("input[value='1']").prop('checked', $(this).prop("checked")).trigger('change');
    $("input[value='2']").prop('checked', $(this).prop("checked")).trigger('change');
    $("input[value='3']").prop('checked', $(this).prop("checked")).trigger('change');
    $("input[value='4']").prop('checked', $(this).prop("checked")).trigger('change');
});


$(document).ready(function() {
    $('.print').val($(this).is(':checked'));
    
    $('input[value="1"]').change(function() {
        if($(this).is(":checked")) {
            $(this).attr("checked", $('.print').text("USA is Checked"));
        } else {
        $('.print').empty();
        }
        $('.print').val($(this).is(':checked'));        
    });
        $('input[value="2"]').change(function() {
        if($(this).is(":checked")) {
            $(this).attr("checked", $('.print2').text("Canada is Checked"));
        } else {
        $('.print2').empty();
        }
        $('.print2').val($(this).is(':checked'));        
    });
        $('input[value="3"]').change(function() {
        if($(this).is(":checked")) {
            $(this).attr("checked", $('.print3').text("UK is Checked"));
        } else {
        $('.print3').empty();
        }
        $('.print3').val($(this).is(':checked'));        
    });
        $('input[value="4"]').change(function() {
        if($(this).is(":checked")) {
            $(this).attr("checked", $('.print4').text("Spain is Checked"));
        } else {
        $('.print4').empty();
        }
        $('.print4').val($(this).is(':checked'));        
    });
        $('input[value="10"]').change(function() {
        if($(this).is(":checked")) {
            $(this).attr("checked", $('.print10').text("Male is Checked"));
        } else {
        $('.print10').empty();
        }
        $('.print10').val($(this).is(':checked'));        
    });
        $('input[value="11"]').change(function() {
        if($(this).is(":checked")) {
            $(this).attr("checked", $('.print11').text("Female is Checked"));
        } else {
        $('.print11').empty();
        }
        $('.print11').val($(this).is(':checked'));        
    });
});
    <p><label><input type="checkbox" id="checkCountries"/> Check Countries</label></p>
    
    <fieldset>
        <legend>Loads of checkboxes</legend>
        <p><label><input type="checkbox" value="1" /> USA</label></p>
        <p><label><input type="checkbox" value="2" /> Canada</label></p>
        <p><label><input type="checkbox" value="3" /> UK</label></p>
        <p><label><input type="checkbox" value="4" /> Spain</label></p>
        <p><label><input type="checkbox" value="10" /> Male</label></p>
        <p><label><input type="checkbox" value="11" /> Female</label></p>
    </fieldset>
    <div class="print"></div>
    <div class="print2"></div>
    <div class="print3"></div>
    <div class="print4"></div>
    <div class="print10"></div>
    <div class="print11"></div>
    

关于javascript - jQuery - 复选框不更新文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62322513/

相关文章:

javascript - 如何获取包含特定文本的div

javascript - 如何在不干扰上方内容的情况下将固定高度的 <div> 锚定到 <td> 的右下角?

javascript - 第一个下拉菜单总是打开的

javascript - 如何创建可拖动的菜单 div(桌面和移动设备)

javascript - 如何防止用户在我的测验中选择多个答案?

javascript - 如何将文本附加到 c3.js 区域?

javascript - jQuery - 5 秒后重定向网页

javascript - 在注入(inject)的 iframe 上添加 HTML

javascript - 如何搜索 HTML 的每一部分

javascript - 交替在字符串的开头和结尾添加一个字符