jquery 用它的值替换每个输入。然后切换/切换回有输入

发布于 2024-10-05 22:54:49 字数 1152 浏览 0 评论 0原文

我有一个由 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>

I have a table created by an AJAX request. Each of the TDs have a Textbox Input inside it. This allows the user to edit content. I want to have a "read only" mode where the user can't edit content (at the user's discretion). For this, I want the user to be able to press a button (radio buttons would also be fine) that replaces each of the Textbox Inputs with a

tag containing the value of the text box as its text. Currently, my code replaces each of Inputs with the text "object Object". (Maybe I haven't converted the values to text strings correctly??) Any tips to let me know what's going wrong would be greatly appreaciated. Thanks!!

This is my current script:

<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>

And this is a snippet of what the HTML could look like:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姐不稀罕 2024-10-12 22:54:49

jQuery 的 each 函数返回调用它的 jQuery 对象(在您的情况下为 $('cells') )。因此,您当前的代码正在将 .cells 选择器匹配的每个元素替换为 jQuery 对象,该对象序列化为“object Object”。

你想要的更像是

$("#permissionsbutton").live('click', function() {
    $(".cells").each(function() {
        $(this).replaceWith($(this).val());
    }
});

jQuery's each function returns the jQuery object it was called on ($('cells') in your case). So your current code is replacing each element matched by the .cells selector with the jQuery object, which is serializing to "object Object".

What you want is something more like

$("#permissionsbutton").live('click', function() {
    $(".cells").each(function() {
        $(this).replaceWith($(this).val());
    }
});
芯好空 2024-10-12 22:54:49
$("#permissionsbutton").live('click',function(){ 
  $('.cells').each(function(){
    var cell=$(this);
    cell.replaceWith('<span class="cells_ro">'+cell.val()+'</span>');
  }); 
});

或者只是禁用单元格:

$("#permissionsbutton").live('click',function(){ 
  $('.cells').attr('disabled',true);
});
$("#permissionsbutton").live('click',function(){ 
  $('.cells').each(function(){
    var cell=$(this);
    cell.replaceWith('<span class="cells_ro">'+cell.val()+'</span>');
  }); 
});

Or just disable cells:

$("#permissionsbutton").live('click',function(){ 
  $('.cells').attr('disabled',true);
});
泼猴你往哪里跑 2024-10-12 22:54:49

怎么样:

$("#permissionsbutton").click(function(){
$('.cells').attr('disabled','disabled');
});

在这里尝试

How about:

$("#permissionsbutton").click(function(){
$('.cells').attr('disabled','disabled');
});

Try it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文