我有一个jQuery函数,它从数据库表中返回一行。当它显示在文本框中时,单词全部一起运行。例如:Everyonepleasebecarefulwhenleavingthebuilding
。我想将单词分开阅读:Every one please be careful when leaving the building
。这来自用户输入,因此用户点击他希望在文本框中显示的任何行。每行包含不同的数据。下面列出的代码是触发事件的原因:
$(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');
});
});
答案 0 :(得分:1)
单击表格行时,在其表格单元格上循环,将每个单词添加到数组中。最后,用空格连接该数组,并将其结果设置为输入字段的值:
$("#statements").on("click", "tr", function(){
var words = [];
$("td", this).text(function(i,v){ words.push( v ); });
$("#txtread").val( words.join(" ") );
});
答案 1 :(得分:0)
您需要遍历每个单元格并单独获取其文本,而不是仅仅抓取行的文本:
$(document).ready(function(){
$("table tr").click(function(){
$("#txttread").val($.map($(this).children('td'), function (item) { return $(item).text() }).join(' '));
});
});
的工作小提琴