JS或Jquery:如何向复选框添加“选项”

时间:2020-04-23 10:21:22

标签: javascript checkbox

以MS Sharepoint形式创建的复选框具有此html结构;

<div class="fd_field " fd_name="purchase_order" style="">
  <div class="fd_title" style="width: 150px;">purchase_order</div>
  <div class="ms-formbody fd_control" fd_type="MultiChoice">
    <span dir="none">
      <table cellspacing="1" cellpadding="0">				
        <tbody>
          <tr>
            <td><span class="ms-RadioText" title="Carica dati">
              <input id="ctl00_ctl40_g_2bbe8d3f_1b61_4103_9ca6_2e943eb28860_purchase_orderField_ctl00_ctl00" type="checkbox" name="ctl00$ctl40$g_2bbe8d3f_1b61_4103_9ca6_2e943eb28860$purchase_orderField$ctl00$ctl00">
              <label for="ctl00_ctl40_g_2bbe8d3f_1b61_4103_9ca6_2e943eb28860_purchase_orderField_ctl00_ctl00">Carica dati</label></span>
            </td>
        </tr>
      </tbody>
    </table>
    </span>
  </div>
</div>

使用ajax,我想将其他数据添加为该复选框中的选项。 检索数据的循环不是问题,但是如何将这些数据附加到复选框的新行中? 过去,我可以编辑下拉字段,但不适用于复选框。

我正在考虑这样的解决方案:

var f = '<tr><td><input type="checkbox" name="xxx" id="ss"><label for="ss">Other</label></td></tr>';
$( ".ms-RadioText" ).parent().append(f);

谢谢

1 个答案:

答案 0 :(得分:1)

你是这个意思吗?

PS:我不会期望一个跨度的表!

var f = '<tr><td><span class="ms-RadioText" title="XXX"><input id="ss" type="checkbox" name="xx"><label for="ss">Other</label></span></td>';
$(".ms-RadioText").closest("tbody").append(f);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fd_field " fd_name="purchase_order" style="">
  <div class="fd_title" style="width: 150px;">purchase_order</div>
  <div class="ms-formbody fd_control" fd_type="MultiChoice">
    <span dir="none">
      <table cellspacing="1" cellpadding="0">				
        <tbody>
          <tr>
            <td><span class="ms-RadioText" title="Carica dati"><input id="xx" type="checkbox" name="xx"><label for="xx">Carica dati</label></span></td>
        </tr>
      </tbody>
    </table>
    </span>
  </div>
</div>