【问题标题】:How to get values on click from table row and show it in input fields如何从表格行中获取点击值并将其显示在输入字段中
【发布时间】:2014-02-02 15:23:39
【问题描述】:

这里我有一个包含数据的 gviz 表:http://jsbin.com/OJAnaji/9 和代码:http://jsbin.com/OJAnaji/9/edit

我还创建了一个带有输入字段的模式窗口。现在我想当我点击表格行时将行数据显示到模式窗口的输入字段中......我该怎么做?

<!-- Button trigger modal -->
<button class="btn btn-success" data-toggle="modal" data-target="#myModal">
Edit selected row</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Add new row</h4>
      </div>
      <div class="modal-body">
        <div class="input-group">
  <span class="input-group-addon">Name</span>
  <input type="text" id-"name" class="form-control" placeholder="Type name">
        </div></br>
        <div class="input-group">
  <span class="input-group-addon">Gender</span>
  <input type="text" id="gender" class="form-control" placeholder="Gender?">
</div></br>
        <div class="input-group">
  <span class="input-group-addon">Age</span>
  <input type="text" id="age" class="form-control" placeholder="Number of age">
</div></br>
        <div class="input-group">
  <span class="input-group-addon">Donuts eaten</span>
  <input type="text" id="donuts_eaten" class="form-control" placeholder="Number of donuts eaten">
</div></br>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

所以: 1.用户点击表格行 2.用户点击按钮“编辑行” 3.用户从行获取值到输入字段

这样可以吗?

对不起,我的英语不好,但我正在努力学习

【问题讨论】:

    标签: javascript jquery html input return-value


    【解决方案1】:

    检查fiddle,这是部分工作,即仅适用于具有白色背景的表格行。更改必要的内容以使其适用于所有行。

    【讨论】:

      【解决方案2】:

      因为我不熟悉你的代码,所以我会写一个我认为适合你的代码,并添加一些 cmets 使其更清晰,如果你需要别的东西,你告诉我。

      $("#YourTableId tbody tr").click(function () {
          //this = the row a user has clicked
          var storedNameInTable = $(this).find("#name").text(); // will get the label with id = name from the same row a user has clicked
          $("#name").val(storedNameInTable); //will take the name and put it on the textbox.
          var self = this;        //saves the row scope
          $("#SaveChanges").click(function () {
              var NameAfterChange = $("#name").val();  //takes the newly changed name
              $(self).find("#name").text(NameAfterChange);  //puts it in the table
              $("#SaveChanges").unbind("click");  //make sure you want call this button again so you want have more than 1 click event
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2017-12-06
        • 2022-08-23
        • 2019-10-26
        • 1970-01-01
        • 2013-12-09
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        相关资源
        最近更新 更多