如何从表行中单击获取值并在输入字段中显示它

时间:2014-02-02 15:23:40

标签: javascript jquery html input return-value

这里我有一个包含数据的gviz表:http://jsbin.com/OJAnaji/9和CODE: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.user单击“编辑行”按钮 3.User从行到输入字段获取值

这可能吗?

抱歉我的英语不好但我正在努力学习

2 个答案:

答案 0 :(得分:1)

选中此fiddle,这部分有效,即仅适用于具有白色背景的表格行。更改必要的内容以使其适用于所有行。

答案 1 :(得分:1)

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

$("#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
    });
});