什么一直在点击时删除文本输入值?

时间:2014-09-23 16:44:52

标签: javascript jquery html text focus

我意识到我几乎要复制一个excel ;在插件中,预期的行为是: 单击a时,包含值的a将被替换为get get相同的值,因此可以进行编辑。它有效,但问题是当点击新创建的文本输入时,值被重置或删除为''。

我不知道那是什么,我已经尝试了事件监听器,我已经尝试过。在焦点上单击焦点,点击,点击,因为它似乎重新点击文本框会触发删除它的东西。我使用了F12事件监听器并进入了一切,但我认为它可能是jquery默认情况下自己做的事情,但我想知道是否知道是什么导致了这种行为。

$( "td" ).click(function(event)
{   
    var value = $( this ).text(); // gets the text inside section inside the td
    var column = $( this ).get( 0 ).id; //gets id="" of the td that is how I indentify columns
    var parent = $( this ).parent().get( 0 ).id; //gets the id of the row <tr id="x"><td id=">

    if( column == "a" || column == "b" )
    {
        return;
    } 
    else if ( column == "d" || column == "c" )
    {
        $( this ).children().replaceWith( '<input type=email value=' + value + '>' );
        $( this ).children().focus();
    }
    else
    {
        $( this ).children().replaceWith( '<input type=text value="' + value + '">' );
        $( this ).children().focus();           
    }

    $( this ).children().focusout(function(event){
        var new_value = $( this ).val();
        $( this ).parent().addClass( "edited" );
        $( this ).replaceWith( '<section>' + new_value + '</section>' );
});

类似的问题已经被问到here,但它并没有解决我的问题,也没有解决我的问题。他们建议他。替换新的html而不是注入它。所以也许使用“.replaceWith()会有问题吗?”... idk,我到处搜索。

1 个答案:

答案 0 :(得分:0)

您正在使用值为{{text} /的输入替换单元格 因此.text()现在什么也不返回。

示例:

html:
<table>
<td id="only_text"><p>some text</p></td>
<td id='input_text'><input type="text" value="some text 2"/></td>
</table>

js:
$(function(){
    console.log($('#only_text').text());
    console.log($('#input_text').text() +'');
})

你可以在这个小提琴中看到这个例子:http://jsfiddle.net/qzbrzm4x/(打开你的控制台并运行)