首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在textbox中转换Td值以编辑和显示Td值

问在textbox中转换Td值以编辑和显示Td值
EN

Stack Overflow用户
提问于 2013-01-19 18:29:27
回答 1查看 776关注 0票数 0

我有一列作为备注,我希望用户让输入备注任何他们想要的,它应该显示每次他们看到这个表我搜索了很多,最后使用这个代码,但它不工作,当用户点击备注,它应该是可编辑的,但它不工作

代码语言:javascript
复制
$(document).ready(function() {
$('#mylist_remark').on('click', '.Edit', function() {
        $(this).closest('tr').find('td:eq(1)').each(function() {
            // replace the existing text with a textbox containing that text
            var existingVal = $(this).text();
            $(this).html('<input type="text" value="' + existingVal + '" >');
        });
    });

    // when the user is finished editing, change the value and remove the textbox,and display edited text
    $('#mylist_remark').on('focusout', 'td input', function() {
        $(this).parent().html( this.value );
    });
});

脚本

代码语言:javascript
复制
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

表代码

代码语言:javascript
复制
    <table id="mylist_remark">
    <caption>SELECTED CANDIDATE LIST</caption>
    <!-- headings -->
        <tr>
          <th> </th>
          <th>REMARK</th>
        </tr>
   <?php $i=0;
    while($data_set = mysql_fetch_array($mresult_set))
        {     
            echo "<tr id=\"{$listrowid}\">";                                
            echo "<td><a href\"\" class=\"Edit\">remark</a></td>";
            echo "</tr>";
            $i++;                       
            }?>
    </table>

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-01-19 18:52:46

您是否考虑过使用"contenteditable“属性,而不是所有这些操作?

代码语言:javascript
复制
<td class="editable" contenteditable="true">Something</td>

$("#mylist_remark .editable").bind("blur", function(e) { 
  console.log($(e.target).text());
  $.post("/somewhere", {remark: $(e.target).text(), candidate_id: $(e.target).nearest("tr").attr("id")}, function() { console.log("success"); });
  //save the result to the server or whatever you need to do to make it persist
});

您还需要绑定"enter“和"escape”按键事件。

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

https://stackoverflow.com/questions/14413369

复制
相关文章

相似问题

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