具有TD和Input的Jquery Map功能
Jquery Map function with TD and Input
我正在尝试组合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();
});
相关文章:
- 用Jquery map和moment js制作一个简单的时间线
- 鉴于 for..在构造中,库提供的函数(如 jQuery.map() 或 _.each())有什么用
- jQuery Map 从多个源返回 JSON 对象
- 使用 jQuery.map 后使用 JSON.stringify 的问题
- Chrome RangeError:使用jQuery$.map时超过了最大调用堆栈大小
- jQuery“map”将中的数组转换为“dom”元素
- 如何在纯JS中实现jQuery.map()的等效功能
- 为什么我的jQuery.map不能在这里组合我的数组
- jquery map 数组在迭代器中返回 NaN
- 如何使用jQuery.map递归修改嵌套对象
- 如何使jQuery .map只向数组添加填充的输入
- javascript .replace and jQuery .map
- 解析类似jquery.map()的数组
- 满足条件时停止Jquery.map
- 具有TD和Input的Jquery Map功能
- 使用JQuery Map更新居中和缩放
- jQuery.map()返回而未定义
- 如何从jQuery.map获取关联数组作为输出
- Jquery map将虚线属性列表转换为对象
- 为什么jQuery map会自动将返回的数组平面化