首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在jQuery中添加带有按钮单击的文本框

无法在jQuery中添加带有按钮单击的文本框
EN

Stack Overflow用户
提问于 2014-10-02 02:38:19
回答 1查看 339关注 0票数 0

我想在表> td中添加文本框,选中复选框,但不起作用。我该怎么做?我是jQuery的新手。

这是我的代码html:

代码语言:javascript
复制
<table id="list_lab" class="checkbox-inline">
<tr>
    <td><input id="list_lab_0" type="checkbox" name="list_lab$0" checked="checked" value="Electrolyte (Lab)"><label for="list_lab_0">Electrolyte (Lab)</label></td>
</tr><tr>
    <td><input id="list_lab_1" type="checkbox" name="list_lab$1" checked="checked" value="Creatinine (plus eGFR)"><label for="list_lab_1">Creatinine (plus eGFR)</label></td>
</tr><tr>
    <td><input id="list_lab_2" type="checkbox" name="list_lab$2" value="Blood Urea Nitrogen"><label for="list_lab_2">Blood Urea Nitrogen</label></td>
</tr><tr>
    <td><input id="list_lab_3" type="checkbox" name="list_lab$3" value="Complete Blood Count"><label for="list_lab_3">Complete Blood Count</label></td>
</tr><tr>
    <td><input id="list_lab_4" type="checkbox" name="list_lab$4" value="Dengue NS1 Ag"><label for="list_lab_4">Dengue NS1 Ag</label></td>
</tr><tr>
    <td><input id="list_lab_5" type="checkbox" name="list_lab$5" value="Influenza A/B A/(H1N1) Screening"><label for="list_lab_5">Influenza A/B A/(H1N1) Screening</label></td>
</tr><tr>
    <td><input id="list_lab_6" type="checkbox" name="list_lab$6" value="Urine Exam"><label for="list_lab_6">Urine Exam</label></td>
</tr>

这是我的剧本:

代码语言:javascript
复制
$(document).ready(function(){    
    $('#btn_check').click(function () {
        var length = 0;
        $('#list_lab').find('tr').each(function(){
            var row = $(this);
            if(row.find('input[type="checkbox"]').is(':checked')){
                $('#list_lab').find(td).append('<input type="text" name="mytext[]" id="txt_row '" + length + "'" />')
            }
            length++;
        });
    });

});  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-02 02:55:05

这是工作代码。单击下面的按钮运行它。

这一行:

代码语言:javascript
复制
$('#list_lab').find(td).append('<input type="text" name="mytext[]" id="txt_row '" + length + "'" />')

应该是:

代码语言:javascript
复制
$('#list_lab').find('td').append('<input type="text" name="mytext[]" id="txt_row_"' + length + '" />');

代码语言:javascript
复制
$(document).ready(function() {
  function create_inputs() {
    var length = 0;
    $('#list_lab').find('tr').each(function() {
      var row = $(this);
      if (row.find('input[type="checkbox"]').is(':checked')) {
        // first check if you've already added an input to this row
        if ($(this).find('input[type="text"]').length == 0) {
          $(this).find('td').append('<input type="text" name="mytext[]" id="txt_row_"' + length + '" />');
        }
      } else {
        // if this is not checked, then remove any inputs that you might have added
        $(this).find('input[type="text"]').remove();
      }
      length++;
    });

  }

  // set this to run whenever you click a checkbox
  $('input[type="checkbox"]').click(function() {
    create_inputs();
  });
  
  // and also run once when you first load
  create_inputs();

});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="list_lab" class="checkbox-inline">
  <tr>
    <td>
      <input id="list_lab_0" type="checkbox" name="list_lab$0" checked="checked" value="Electrolyte (Lab)">
      <label for="list_lab_0">Electrolyte (Lab)</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="list_lab_1" type="checkbox" name="list_lab$1" checked="checked" value="Creatinine (plus eGFR)">
      <label for="list_lab_1">Creatinine (plus eGFR)</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="list_lab_2" type="checkbox" name="list_lab$2" value="Blood Urea Nitrogen">
      <label for="list_lab_2">Blood Urea Nitrogen</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="list_lab_3" type="checkbox" name="list_lab$3" value="Complete Blood Count">
      <label for="list_lab_3">Complete Blood Count</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="list_lab_4" type="checkbox" name="list_lab$4" value="Dengue NS1 Ag">
      <label for="list_lab_4">Dengue NS1 Ag</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="list_lab_5" type="checkbox" name="list_lab$5" value="Influenza A/B A/(H1N1) Screening">
      <label for="list_lab_5">Influenza A/B A/(H1N1) Screening</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="list_lab_6" type="checkbox" name="list_lab$6" value="Urine Exam">
      <label for="list_lab_6">Urine Exam</label>
    </td>
  </tr>
</table>
<button id="btn_check">click me</button>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26153717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档