2014-09-23 57 views
0

我意识到我几乎复制一个excel表格;在一个插件,预期的行为是: 被点击一个含有值时,它内被替换的是得到是相同的值,因此可以进行编辑。它的工作原理,但问题是当点击新创建的文本输入时,该值被重置或擦除为''。什么一直在删除点击文本输入值?

我不知道那是什么,我试过了事件监听器,我试过.unbind焦点,点击,点击,焦点,因为它似乎重新点击文本框会触发删除它的东西。我用F12事件侦听器和步入一切,但我认为这可能是对自己的东西jQuery也默认,但我想知道,如果anyonw知道是什么原因造成这种现象。

$("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,但它并没有解决我的问题,也没有解决我的问题。他们建议他.append新的html,而不是注入它。因此,也许有使用的问题“.replaceWith()?” ...... IDK的,我搜索无处不在。

+0

你能提供他的HTML去与JavaScript代码http://jsfiddle.net/qzbrzm4x/(打开你的控制台和运行)?或者在jsfiddle.net中加入一个例子 – Silkster 2014-09-23 16:49:51

+0

http://jsfiddle.net/qzbrzm4x/1/在那里你可以看到行为,在选择光标区域时,它也停在空格处,我注意到了,对此也有任何想法? – Dracovic5 2014-09-23 23:20:02

回答

0

你有具有价值= {文本}输入更换电池/ 因此的.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() +''); 
}) 

你可以看到在这个小提琴的例子:

+0

不错,不错....所以每个新点击进入触发了​​点击,因为它是​​(OBV)内。那我该怎么去解决呢?我是js的新手,所以我习惯于以线性方式编写代码并立即运行。因此,我从来没有想过在没有完成整个功能的情况下触发另一个或相同的td(所以不清楚) – Dracovic5 2014-09-23 19:27:58

+0

您可以点击查看孩子,看看它是否是输入,如果是,则执行其他操作(即什么也不做,从函数返回)...解决方案取决于你。 当您从输入中离开焦点时,您可以将输入更改回文本,这将为您提供细胞的良好生命周期 - 它只会在编辑时暂时处于输入状态 – Etai 2014-09-23 19:45:49