具有TD和Input的Jquery Map功能

Jquery Map function with TD and Input

本文关键字:Jquery Map 功能 Input TD 具有      更新时间:2023-09-26

我正在尝试组合td中的单词和在文本框中输入的文本。

<table width="100%">
<tr>
<td class="CommentWords" width="25%">jsakldjs </td>
  <td class="Label" width="85%"> <input class="Textcomment"
      id="txtCommentWords_3" type="text" width="550px">
</td>
</tr>
<tr class="NormalItem">
    <td class="CommentWords" width="25%">klsajdksajd </td>
    <td class="Label" width="85%"> <input class="Textcomment"
        id="txtCommentWords_4" type="text" width="550px"> </td>
</tr>
</table>

因为我需要从"commentWords"answers"TextComment类"中选择所有单词,并关联值。小提琴手链接。

例如:

输出将是

jsakldjsÅInputTextvalue¶//Å将是字段分隔符,¶将是行分隔符

这是我尝试过的,但到目前为止,它将返回textbox值或td值。我需要用字段和行分隔符获得所有td值和相应的文本值

var list = $(".CommentWords, .Textcomment", this).map(function() {
                            return $(this).val();
                        });

你应该做一些类似的事情

var list = $(".CommentWords, .Textcomment", $('#table')).map(function() {
    var $this = $(this);
    if ($this.is('td')) {
        return $this.text();
    } else {
        return $this.val();
    }
});

打印结果

for(i = 0; i < list.length; i++){
    var current  = list[i];
    $('#result').append(current);
    if(i % 2 !== 0){
        $('#result').append(' [end of line] ');
    }else{
         $('#result').append(' [td - input separetor] ');
    }
}

如果您需要执行字符串

 var finalString = '';
 for(i = 0; i < list.length; i++){
    var current  = list[i];
    finalString  += current;
    if(i % 2 !== 0){
        finalString += ' [end of line] ';
    }else{
        finalString += ' [td - input separetor] ';
    }
 }

小提琴在这里http://jsfiddle.net/23hgy/2/

使用text()函数检索<td>的文本。我做了一把小提琴给你看:

http://jsfiddle.net/U9xnj/1/

基本上,你可以这样做,但它不是很可读:

var list = $(".CommentWords, .Textcomment").map(function() {
                            return $(this).text() || $(this).val();
                        });