2012-05-28 157 views
0

我有一个jQuery函数从数据库表中返回一行。当它显示在文本框中时,这些单词全部一起运行。例如:Everyonepleasebecarefulwhenleavingthebuilding。我想将这些文字分开阅读:Every one please be careful when leaving the building。这来自用户输入,所以用户点击他希望在文本框中显示的任何行。每行包含不同的数据。下面列出的代码是什么触发事件:如何在jQuery中的单词之间添加空格?

$(document).ready(function() { 
    $("table tr").click(function(){ 
     $("#txttread").val($(this).text()); 
    }); 
}); 

$(document).ready(function() { 
    $('.pickme tr').not(':first').hover(
     function() { $(this).addClass('highlight'); }, 
     function() { $(this).removeClass('highlight'); } 
    ).click(function() { 
     $('.selected').removeClass('selected'); 
     $(this).addClass('selected').find('input').attr('checked','checked'); 
    }); 
}); 
+2

或者一个更重要的问题

$(document).ready(function(){ $("table tr").click(function(){ $("#txttread").val($.map($(this).children('td'), function (item) { return $(item).text() }).join(' ')); }); }); 

工作小提琴,你怎么知道在哪里添加空间? –

+1

使用从数据库检索信息的服务器端代码修复它可能更容易。在你的jQuery中,我没有看到任何会导致单词一起运行的东西。如果您向我们展示更多代码,则可能会看到问题:) – FireCrakcer37

+0

数据位于可以有8列的表格中。每个单词都在表格中的自己的单元格中,每行可以不同,但​​是它们都具有8个单元格。我已经能够提取任何单元格被点击,并且我得到了该单元格中的任何内容。当我要求整行时,它就像上面的例子那样连接在一起。 – Urob

回答

1

当表行被点击时,循环在其表细胞,加入他们的每一个字的阵列。最后,用空格加入该阵列,并将其结果作为输入字段的值:

​$("#statements").on("click", "tr", function(){ 
    var words = []; 
    $("td", this).text(function(i,v){ words.push(v); }); 
    $("#txtread").val(words.join(" ")); 
});​​​​​​​​​​ 

小提琴:http://jsfiddle.net/EXPBp/1/

相关问题