2017-09-05 63 views
-1

我想在jQuery選擇器中放置一個變量。不過,我假設我打破了語法規則的某種形式。請讓我知道你是否能發現錯誤:jQuery選擇器錯誤中的變量

var names = ["JakeP97", "Trishy", "Puffs", "Evilgenious", "Joeyc", "TheKid"]; 
var ballots = ["#book1", "#book2", "#book3", "#book4", "#book5", "#book6"]; 

function splitName(plName,ballotNum) { 
    var halfplName = Math.round(plName.length/2); 
    var firstplname = plName.substr(0, halfplName); 
    var lastplName = plName.substr(halfplName, plName.length); 
    $(ballotNum 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname); 
    $(ballotNum 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname); 
} 

for (i=0; i<ballots.length; i++) { 
    splitName(names[i],ballots[i]); 
} 

錯誤發生在下面幾行: $(ballotNum 'ul.hardcover_front')

期望的結果將是: $('#book1 ul.hardcover_front')$('#book2 ul.hardcover_front')等等,所有的方式。

預先感謝您!

+0

它是$(ballotNum +'ul.hardcover_front'),你缺少一個+來加入一個字符串。還要確保ballotNum在前面有一個散列。 – Adriani6

回答

4

您需要連接字符串和變量。

$(ballotNum + ' ul.hardcover_front').find('li:nth-child(2)').html(firstplname); 
1

您錯過了+標誌。既然你是一個字符串串聯的變量,你需要使用+標誌:

$(ballotNum + 'ul.hardcover_front') 

所以這行:

$(ballotNum 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname); 
$(ballotNum 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname); 

將變更爲:

$(ballotNum + 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname); 
$(ballotNum + 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname); 
1
$(ballotNum + 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname); 
$(ballotNum + 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname); 

我覺得你忘了加+