获取JavaScript中CheckboxSelectMultiple小部件上选中复选框的ID的Django代码。

huangapple go评论94阅读模式
英文:

Django get id of the selected checkbox on CheckboxSelectMultiple widget in Javascript

问题

I have checkboxes in Django from a form:

  1. testrever = forms.MultipleChoiceField(required=False, widget=forms.widgets.CheckboxSelectMultiple())

I would like to return only the ID (and then the value) of the last selected checkbox on .change event only when it's checked (when it's unchecked I don't want anything to happen).

I managed to do so with the following (thanks to this answer):

Stack Overflow Link

  1. $('#id_testrever_0').on("change", function() {
  2. var testrever =[];
  3. var a = $("#id_email").val();
  4. if ($('#id_testrever_0').is(':checked')) {
  5. testrever = $('#id_testrever_0').val();
  6. $("input[name=email]").val(a + testrever);
  7. }
  8. });
  9. $('#id_testrever_1').on("change", function() {
  10. var testrever =[];
  11. var a = $("#id_email").val();
  12. if ($('#id_testrever_1').is(':checked')) {
  13. testrever = $('#id_testrever_1').val();
  14. $("input[name=email]").val(a + testrever);
  15. }
  16. });

But I would like to achieve it in a more elegant way to minimize the code length and avoid the repetitions, maybe by using:

  1. $("input:checkbox[name=testrever]").on("change", function() {
  2. $("input:checkbox[name=testrever]:checked").each(function() {
  3. // Your code here
  4. });
  5. });

if it's possible.

英文:

I have checkboxes in Django from a form :

  1. testrever = forms.MultipleChoiceField(required=False,widget=forms.widgets.CheckboxSelectMultiple())

I would like to return only the ID (and then the value) of the last selected checkbox on .change event only when it's checked (when it's unchecked I don't want anything to happen).

I managed to do so with the following (thanks to this answer) :

https://stackoverflow.com/questions/76291806/django-how-to-get-value-of-checkboxselectmultiple-in-javascript-in-template

  1. $('#id_testrever_0').on("change", function() {
  2. var testrever =[]
  3. var a = $("#id_email").val();
  4. if ($('#id_testrever_0').is(':checked')) {
  5. testrever = $('#id_testrever_0').val();
  6. $("input[name=email]").val(a + testrever)
  7. }
  8. })
  9. $('#id_testrever_1').on("change", function() {
  10. var testrever =[]
  11. var a = $("#id_email").val();
  12. if ($('#id_testrever_1').is(':checked')) {
  13. testrever = $('#id_testrever_1').val();
  14. $("input[name=email]").val(a + testrever)
  15. }
  16. })

But I would like to achieve it in a more elegant way to minimise the code lenght and avoid the repetitions.

maybe by using

  1. $("input:checkbox[name=testrever]").on("change", function()
  2. $("input:checkbox[name=testrever]:checked").each(function()

if it's possible.

答案1

得分: 1

我不完全确定我是否完全理解你想要的内容。但如果你想要将 #id_email 的值与选中的复选框的值追加在一起,你可以使用以下代码:

  1. $('$id_testrever input:checkbox').on('change', function() {
  2. if(this.checked) {
  3. const element = $('$id_email');
  4. element.val(element.val() + this.value);
  5. }
  6. });
英文:

I'm not entirely convinced I I fully understand what you want. But if you want to append the value of #id_email with the value of the checkbox checked, you can use:

  1. $('#id_testrever input:checkbox').on('change', function() {
  2. if(this.checked) {
  3. const element = $('#id_email');
  4. element.val(element.val() + this.value);
  5. }
  6. });

huangapple
  • 本文由 发表于 2023年5月21日 03:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定