【问题标题】:if the checkbox is checked then radio disabled = false and not checked then the radio is disabled = true如果选中该复选框,则无线电禁用 = false 并且未选中则无线电被禁用 = true
【发布时间】:2021-09-08 03:17:37
【问题描述】:

我有一个表单,其中包含一个复选框和一个单选按钮(禁用=true)。当复选框选中则禁用单选按钮为假,当复选框未选中则单选按钮被禁用

id="rp0" 中的数字 0 从循环或侧服务器获取

<input type="checkbox" id="rp0" value="1011000646">

<input type="radio" disabled="disabled" id="trx0" value="0">
let temp = [];
 function prepAcc() {
        $("#tableAcc").find("tr:gt(0)").remove();
        $.ajax({
          url: urlAjax + $("#companyId").val(),
          async: true,
          success: function (result) {
            console.log("list acc result: " + result + ", type: " + (typeof result));
            console.log(result);
            temp.push(result); // temp obj

            var s = "";
            $.each(result.data, function (index, obj) {


              // let check = true
              //   < input disabled = { check? disabled: enabled } >

              $("#tableAcc").find('tbody')
                .append($('<tr>')
                  .append($('<td>').append(obj.cif))
                  .append($('<td>').append(obj.accNumber))
                  .append($('<td>').append(obj.accName))
                  .append($('<td>').append(obj.accCategoryName))
                  .append($('<td>').append($('<input>')
                    .attr('type', 'checkbox')
                    .attr('id', 'rp' + index)
                    .attr('value', obj.accNumber)
                  ))
                  .append($('<td>').append($('<input>')
                    .attr('type', 'radio')
                    .attr('disabled', 'disabled')
                    .attr('id', 'trx' + index)
                    .attr('value', 0)
                  ))
                );
              if (index > 0)
                s += "|";
              s += obj.accId;
            });
            $("#allAccString").val(s);
          },
          error: function (xhr) {
            alert("An error occured: " + xhr.status + " " + xhr.statusText);
          }
        });

        ajdTrx();
      }

this picture

问题是当复选框未选中时,收音机未禁用=false,并且它不会进入 for 循环或映射。我喜欢这个

red block is temp with temp obj

function ajdTrx() {
        for (let index = 0; index < temp.length; index++) {
          console.log('IN LOOP');
          let checker = document.getElementById('rp' + index);
          let sendbtn = document.getElementById('trx' + index);

          checker.onchange = function () {
            if (this.checked) {
              sendbtn.disabled = false;
            } else {
              sendbtn.disabled = true;
            }
          }
        }

        // temp.map((data, index) => {
        //   console.log('IN MAP');
        //   let checker = document.getElementById('rp' + index);
        //   let sendbtn = document.getElementById('trx' + index);
        //   checker.onchange = function () {
        //     if (this.checked) {
        //       sendbtn.disabled = false;
        //     } else {
        //       sendbtn.disabled = true;
        //     }
        //   }
        // });
        // var gf = $("input[id='rp']:checked").val();
        // console.log('TRX: ' + gf);


        console.log('TRX: ');
        console.log(temp);
      }

【问题讨论】:

    标签: javascript html jquery loops


    【解决方案1】:

    它没有进入 for 循环,因为在调用 ajdTrx(); 时临时数组中没有数据。在成功回调中移动ajdTrx(); 调用并将结果数组作为参数传递给它。除了那个其他代码似乎工作正常https://stackblitz.com/edit/js-f32id2?file=index.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2014-07-11
      相关资源
      最近更新 更多