2014-01-16 44 views
-1

我正在使用$('#tbl-' + no + ' tr')(其中var no = 'one')來計算表中的長度爲<tr>,這有時可以正常工作,但有時會出錯。<table><tr>在JQuery中的選擇器

Uncaught Error: Syntax error, unrecognized expression: #tbl-[object HTMLSelectElement] 

有沒有另外一種方法來選擇<tr>

小提琴:Fiddle

在我的項目

JavaScript函數:

function createAgentList(list, campaign) { 
alert("table rows : " + $('#liveAgentTable-' + campaign + ' tr').length); 
$('#liveAgentTable-' + campaign + ' tr').has('td').remove(); 

for (var i = 0; i < list.length; i++) { 
    var tr = $('<tr class="agnt_tr" value="user"></tr>').appendTo('#liveAgentTable-' + campaign); 
    var radioBtn = $('<input type="radio" name="radioAgent" value="' + list[i].name + '" />" id="radioAgent' + list[i].name + '"'); 
    tr.append(radioBtn); 
    tr.append("<td>" + list[i].name + "</td>"); 
    tr.append("<td>" + list[i].extension + "</td>"); 
    tr.append("<td>" + list[i].skill + "</td>"); 
    tr.append("<td>" + list[i].phoneNumber + "</td>"); 
    tr.append("<td>" + list[i].status + "</td>"); 
    tr.append("<td>" + list[i].callType + "</td>"); 
    tr.append("<td>" + list[i].time + "</td>"); 
} 
} 

HTML表:

<table id="liveAgentTable-<s:property />" class="agent_table" border="1" value="campaignId" style="width: 100%"> 

        <tr> 
         <th>Select</th> 
         <th>Agent Name</th> 
         <th>Extension</th> 
         <th>Skill</th> 
         <th>Phone Number</th> 
         <th>Status</th> 
         <th>Call Type</th> 
         <th>HH:MM:SS</th> 
        </tr> 
       </table> 

<s:property />是廣告活動的名稱是通過遍歷活動清單

未來
+3

有時定義,以便我們能夠重現 –

+0

請提供HTML和JS代碼 –

+1

你的小提琴是工作的罰款... – Myth

回答

1

您是否在代碼的其他地方使用變量no

它的意思是no = [object HTMLSelectElement],它不是一個字符串,而是一個html元素。這讓我想知道你是否在其他地方做過類似var no = $('#choice')之類的東西。

嘗試使用更具描述性的變量。

或者而是試圖做這樣的事情:

$('table:first-child')$('table:nth-child(5)')