2012-05-28 67 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/

相關問題