我有一个由 AJAX 请求创建的表。每个 TD 内部都有一个文本框输入。这允许用户编辑内容。我想要一个“只读”模式,用户无法编辑内容(由用户自行决定)。为此,我希望用户能够按下一个按钮(单选按钮也可以),用包含文本框值作为其文本的
标记替换每个文本框输入。目前,我的代码将每个输入替换为文本“object Object”。 (也许我没有正确地将值转换为文本字符串?)任何让我知道出了什么问题的提示将不胜感激。谢谢!!
这是我当前的脚本:
<script type="text/javascript">
$("#permissionsbutton").live('click',function(){
sss = $('.cells').each(function(){$(this).val()});
$(".cells").replaceWith("<p class='readonlycells' type='text'>" + sss + "</p>");
});
// if this worked, I would write another few lines
// to be able to switch back to having inputs
</script>
这是 HTML 的一个片段:
<div id="readwrite" class="settings">
<h3>Permissions</h3>
<button id="permissionsbutton">Switch to Read Only Mode</button>
</div>
<table><tr>
<td><input class='cells' type=text value='Steve'></td>
<td><input class='cells' type=text value='Mary'></td>
<td><input class='cells' type=text value='Big Bird'></td>
</tr></table>
最佳答案
jQuery 的 each
函数返回调用它的 jQuery 对象(在您的情况下是 $('cells')
)。因此,您当前的代码正在将 .cells
选择器匹配的每个元素替换为 jQuery 对象,该对象序列化为“object Object”。
你想要的更像是
$("#permissionsbutton").live('click', function() {
$(".cells").each(function() {
$(this).replaceWith($(this).val());
}
});
关于jquery 用它的值替换每个输入。然后切换/切换回有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4341411/