使用 jQuery 的可编辑文本功能

发布于 2024-09-28 20:01:25 字数 272 浏览 2 评论 0原文

我有一个图像按钮,当我单击它时,我想要一个特定字段从文本变为可编辑文本字段,有点像动态编辑按钮。

所以我有带有特定 id 的纯文本(即 id="text1"),当我单击按钮时,文本会更改为可编辑字段,可能类似于 $("#text1").hide() ; 然后 $("#field1").show(); 但在中间我需要为字段提供文本的值,然后当我单击按钮保存时应该隐藏输入字段并只显示带有新值的文本。

任何帮助将不胜感激。

谢谢:D

I have an image button and when I click it I want an specific field to go from text to an editable textfield, kinda like a dynamic edit button.

So I have the plain text with certain id (ie. id="text1") and when I click the button, the text changes to an editable field, maybe something like $("#text1").hide(); and then $("#field1").show(); but in between I need to give the field the value of the text, and then when I click the button save I should hide the input field and just show the text with the new value.

Any help will be greatly appreciated.

Thanks :D

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

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

发布评论

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

评论(3

小…楫夜泊 2024-10-05 20:01:25

给定一个输入字段、一个带有 id="text1"paragraph 和一个按钮。

<input type="text" />
<p id="text1">Lorizzle ipsizzle dolizzle sit amizzle, consectetuer adipiscing yo mamma.</p>
<button>Copy</button>

这个简单的 jQuery 将从段落中复制文本,隐藏它并显示带有段落文本的输入。

$("button").click(function() {
   $("input").val(
       $("#text1").hide().text()
   ).show();
});

这是一个示例

只是为了好玩,我编写了一个小脚本,可以启用段落的 功能。只需将 .editable 类添加到任何段落,jQuery 就会处理剩下的事情。我还没有扩展它以允许多次编辑,而且我几乎开始编写保存到数据库的 AJAX 调用,因为我很无聊。 但是因为阳光明媚,我想我宁愿去海滩。这是我的代码和示例。

$(".editable").each(function(i) {
    var $this = $(this);
    $this.attr("id", "orig-" + i);

    var $edit = $("<button />")
    .text("edit")
    .attr("id", "update-" + i)
    .click(function() {
        var $input = $('<input type="text" />')
            .attr("id", "edit" + i)
            .val($this.text());

        var $save = $('<button>Save</button>')
            .click(function() {
                var $new = $("<p />").text($input.val());
                $input.replaceWith($new);
                $(this).hide();
            });
        $(this).replaceWith($save);

        $this.replaceWith($input);
    });

   $(this).after($edit)
});

示例

你真的不需要所有 ID,但如果您要使用新值执行 POST,则可以轻松引用这些元素。

Given an input field, a paragraph with id="text1" and a button.

<input type="text" />
<p id="text1">Lorizzle ipsizzle dolizzle sit amizzle, consectetuer adipiscing yo mamma.</p>
<button>Copy</button>

This simple jQuery will copy the text from the paragraph, hide it and show the input with the text from the paragraph.

$("button").click(function() {
   $("input").val(
       $("#text1").hide().text()
   ).show();
});

Here's a sample.

Just for fun, I've written a small script that enables <editable> functionality for paragraphs. Just add a class of .editable to any paragraph and jQuery takes care of the rest. I haven't extended it to allow multiple edits and I almost started writing AJAX calls that save to the database because I'm bored. But since the sun is shining I thought I'd rather go to the beach. Here's my code and a sample.

$(".editable").each(function(i) {
    var $this = $(this);
    $this.attr("id", "orig-" + i);

    var $edit = $("<button />")
    .text("edit")
    .attr("id", "update-" + i)
    .click(function() {
        var $input = $('<input type="text" />')
            .attr("id", "edit" + i)
            .val($this.text());

        var $save = $('<button>Save</button>')
            .click(function() {
                var $new = $("<p />").text($input.val());
                $input.replaceWith($new);
                $(this).hide();
            });
        $(this).replaceWith($save);

        $this.replaceWith($input);
    });

   $(this).after($edit)
});

SAMPLE

You really don't need all the ID's but if you're gonna do a POST with new values, you can easily refer to the elements.

从此见与不见 2024-10-05 20:01:25

还不错。文本进入文本字段的值区域。因此,从文本区域获取它,将其保存在临时变量中,然后将其放入文本字​​段中。

实际上,你甚至不需要我认为不需要的温度,它应该看起来像

$('#field1').val($('#text1').text)

“注意这是未经测试的”。您可能会发现这篇文章也很有价值。

Not too bad. The text goes into the value area of the text field. So get it from the text area, save it in a temp variable, and put it in the textfield.

Actually, you don't even need the temp I don't think, it should look something like

$('#field1').val($('#text1').text)

Note this is untested. You might find this SO article to be of value as well.

瑾兮 2024-10-05 20:01:25
$(document).ready(function(){

    $('#mod').click(function(){

    $('#text1').html('<input id="no" size="'+$(this).text().length+'" type="text" value="'+ $('#text1').text() + '">');
      
    $('#mod').hide();

    $('#sav').show();

    });

    $('#sav').click(function(){
						     
    // here code to save data in database

    });   
    });    
 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <td id="text1"> text </td>
    <input type="button" id="mod" name="modify"  value="modify" />
    <input type="submit" name="submit" id="sav" value="save" />
    </table>

$(document).ready(function(){

    $('#mod').click(function(){

    $('#text1').html('<input id="no" size="'+$(this).text().length+'" type="text" value="'+ $('#text1').text() + '">');
      
    $('#mod').hide();

    $('#sav').show();

    });

    $('#sav').click(function(){
						     
    // here code to save data in database

    });   
    });    
 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <td id="text1"> text </td>
    <input type="button" id="mod" name="modify"  value="modify" />
    <input type="submit" name="submit" id="sav" value="save" />
    </table>

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