在jquery中连接构建CSV字符串变量

时间:2015-04-21 09:32:50

标签: javascript jquery csv

在jquery中连接以实现" carid1 | engineid1,carid2 | engineid2"的最佳方法是什么?例如:" 244 | 504442 | 882"

$('#tblResults input[id*="ShowRoom"]').each(function(i, item){ 
strValue = $(this).attr("carid") + '|' + $(this).attr("engineid"); 

alert(strValue);

 }).get().join(",");  

下面是html

<TABLE width="100%" class="normal" id="tblResults" border="0" cellSpacing="0" cellPadding="0">
    <TBODY>
    <TR class=header>
    <TH class=sortLPad title="Sort by URN" onclick="reSort('0')" align=left>URN </TH>

    <TH class=sortLPad title="Sort by ShowRoom" onclick="reSort('5')" align=left>Show Room</TH></TR>

    <P></P>

    <TR EventId="504">
    <TD id=URN noWrap>23KV9878788</TD> 

    <TD id="ShowRoom" class=lPad><INPUT onfocusin=clearField(this); id="ShowRoom0" onfocusout=resetField(this); onkeypress=validate(this) maxLength=1 value=1 size=1 version="2000120420142859" elementname="ShowRoom" engineid="504" carid="244"></TD>

    </TR>

    <TR EventId="403">
    <TD id=URN noWrap>889878744</TD> 

    <TD id="ShowRoom" class=lPad><INPUT onfocusin=clearField(this); id="ShowRoom2" onfocusout=resetField(this); onkeypress=validate(this) maxLength=1 value=1 size=1 version="2000120420142859" elementname="ShowRoom" engineid="882" carid="442"></TD>

    </TR>

    </TBODY>
</TABLE>

1 个答案:

答案 0 :(得分:0)

您当前的.each方法会返回您迭代的每个input元素;因此.get().join(',')将不起作用。而是将值推送到数组中,然后使用join(',')连接。

var arr = [];
$('#tblResults input[id*="ShowRoom"]').each(function(i, item){ 
strValue = $(this).attr("carid") + '|' + $(this).attr("engineid");
    arr.push (strValue)
 });

console.log (arr.join (',')) // returns 244|504,442|882
相关问题