最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery div to textbox onclick and back to div - Stack Overflow

programmeradmin2浏览0评论

If you have been on the new PHP myadmin you can click on a field and edit it then when you click away it changes back to a div, I thought this could be found on the web easy but aprently not, so I tryed to make it and I'm not very good with javascript so I failed. Heres what i got so far.

HTML:

<td id="td_1"><input type="hidden" value="0" />0</td>

Javascipt:

$("#td_1").click(function() {
    $input = $("#td_1");
    $field = $('<input type="text" id="txt_1" maxlength="1" size="1"/>').attr({
        id: $input.id,
        name: $input.name,
        value: $input.val()
    });
    $input.after($field).remove();
});

It adds the textbox but does not add the value to it, and stuck on how to change it back

Thanks for any help :)

If you have been on the new PHP myadmin you can click on a field and edit it then when you click away it changes back to a div, I thought this could be found on the web easy but aprently not, so I tryed to make it and I'm not very good with javascript so I failed. Heres what i got so far.

HTML:

<td id="td_1"><input type="hidden" value="0" />0</td>

Javascipt:

$("#td_1").click(function() {
    $input = $("#td_1");
    $field = $('<input type="text" id="txt_1" maxlength="1" size="1"/>').attr({
        id: $input.id,
        name: $input.name,
        value: $input.val()
    });
    $input.after($field).remove();
});

It adds the textbox but does not add the value to it, and stuck on how to change it back

Thanks for any help :)

Share Improve this question asked May 24, 2013 at 21:08 RobRob 3122 gold badges3 silver badges15 bronze badges 2
  • 1 Check out the contenteditable attribute in HTML5. It may help: <div id="example-one" contenteditable="true">CLICK AND EDIT ME</div>. – acdcjunior Commented May 24, 2013 at 21:11
  • Couldn't you leave the div there and just add the textbox inside (moving the text from the div to the textbox)? $("#myDiv").html="<textarea id="myTextbox">"+$("#myDiv").text+"</textarea>"; – David Starkey Commented May 24, 2013 at 21:12
Add a comment  | 

3 Answers 3

Reset to default 9

I would have a hidden textbox that you show when you click on the text value. Working example

HTML

<td id="td_1"><input id="txtBox" type="textbox" value="0" style="display:none;" /><span id="txtBoxValue">0</span></td>

jQuery

$(function() {
    $('#txtBoxValue').on('click', function() {
        $(this).hide(); //hide text
        $('#txtBox').show(); //show textbox
    });

    $('#txtBox').on('blur', function() {
        var that = $(this);
        $('#txtBoxValue').text(that.val()).show(); //updated text value and show text
        that.hide(); //hide textbox
    });
});

Try this fiddle, it works on multiple span's and input's.

<div class="editDIV">
    <span class="editESPAN" style="display:block;">asd</span>
    <input class="editINPUT" style="display:none;" type="text"/>
</div>
<div class="editDIV">
    <span class="editESPAN" style="display:block;">asd</span>
    <input class="editINPUT" style="display:none;" type="text"/>
</div>

<script type="text/javascript">

$(document).ready(function () {

$(".editDIV").click(function(){
    $(this).find("span")[0].style.display="none";
    $(this).find("input")[0].style.display="block";
    $(this).find("input")[0].focus();
});

$(".editINPUT").blur(function(){
    $(this)[0].style.display="none";
    $(this).prev()[0].innerText=$(this)[0].value;
    $(this).prev().show();
});

});
</script>
$("#td_1").click(function() {
    $input = $("#td_1");
    $field = $('<input type="text" id="txt_1" maxlength="1" size="1"/>').attr({
        id: $input.id,
        name: $input.name,
        value: $input.text()
    });
    $input.after($field).remove();
});
发布评论

评论列表(0)

  1. 暂无评论