如何使用jquery跳过行:不

时间:2016-01-11 13:43:30

标签: jquery

我坚持使用jQuery :not跳过行的问题。在这里,我动态添加行,并让一些<tr>有一个类addBgColor。点击submit后,我想将所有表数据转换为xml结构数据,但行包含"addBgColor"类,这里是我试过的代码,请帮帮我

var elements = $(".multipleData tr:not(.addBgColor) td input:checked");
    var size = elements.size();
    bodyRowSet = bodyRowSet + "<count>" + size + "</count>";
    elements.each(function() {    
            var tdValue = $(this).parent().next('td').html().trim();        
        bodyRowSet = bodyRowSet + "<row><eventid>";          
            bodyRowSet = bodyRowSet +  tdValue;
                bodyRowSet = bodyRowSet + "</eventid></row>";
    });

2 个答案:

答案 0 :(得分:3)

纠正你的html筑巢,一切都会运行得很好。没有tbody开始标记&amp;所有tr都没有&#39;有结局。这里修复了jsFiddle

&#13;
&#13;
$(document).ready(function(){
    $("#idXmlData").click(function(){
    var xmlStart =  "<?xml version=\"1.0\" encoding=\"UTF-8\">";
    var xmlFirstRowSet = "<event>";
	  var xmlLast="</event>";
    var headerRowSet='';
    xmlFirstRowSet = xmlFirstRowSet + headerRowSet;
    var bodyRowSet ='';
    
    var elements = $(".multipleData tr:not(.addBgColor) td input:checked");
    var size = elements.size();
    bodyRowSet = bodyRowSet + "<count>" + size + "</count>";
    elements.each(function() {    
    		var tdValue = $(this).parent().next('td').html().trim();        
        bodyRowSet = bodyRowSet + "<row><eventid>";          
            bodyRowSet = bodyRowSet +  tdValue;
        		bodyRowSet = bodyRowSet + "</eventid></row>";
    });
    
    var xml = xmlStart + xmlFirstRowSet + bodyRowSet+xmlLast;
     window.alert(xml);  	
    });
  });
  
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="multipleData" style="border:1px solid #000000;">

<tbody>
	<tr>	
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test1
		</td>
      </tr>
	<tr  class="addBgColor">
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test2
		</td>
      </tr>
	<tr>
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test3
		</td>
    </tr>
	<tr>	
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test4
		</td>
	</tr>

      
     </tbody>
</table>

  <input type="button" name="" value="XML Data" id="idXmlData" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

作为您的jsfiddle代码,请更新表格的HTML代码不正确请更新至以下内容:

 <table class="multipleData" style="border:1px solid #000000;">


<tr>    
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test1
    </td>
  <td>
</tr>
<tr  class="addBgColor">
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test2
    </td>
  <td>
</tr>

<tr>    
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test3
    </td>
  <td>
</tr>

<tr>    
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test4
    </td>
</tr>

 </tbody>