Javascript jQuery选择器中的变量错误

Javascript jQuery选择器中的变量错误,javascript,jquery,Javascript,Jquery,我试图在jQuery选择器中放入一个变量。然而,我假设我打破了某种形式的语法规则。如果您能发现错误,请告诉我: var names = ["JakeP97", "Trishy", "Puffs", "Evilgenious", "Joeyc", "TheKid"]; var ballots = ["#book1", "#book2", "#book3", "#book4", "#book5", "#book6"]; function splitName(plName,ballotNum) {

我试图在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]);
}
var name=[“JakeP97”、“trisy”、“Puffs”、“Evilgenious”、“joyc”、“TheKid”];
var选票=[“第1册”、“第2册”、“第3册”、“第4册”、“第5册”、“第6册];
函数splitName(plName,ballotNum){
var halfplName=Math.round(plName.length/2);
var firstplname=plName.substr(0,半plName);
var lastplName=plName.substr(半plName,plName.length);
$(ballotNum'ul.hardcover_front').find('li:nth child(2)').html(firstplname);
$(ballotNum'ul.hardcover_back').find('li:nth child(1)').html(lastplname);
}

对于(i=0;i您需要连接字符串和变量

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

您缺少
+
符号。由于您正在用字符串连接变量,因此需要使用
+
符号:

$(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);
我想你忘了添加
++

它是$(ballotNum+'ul.hardcover\u front'),你缺少一个+来连接字符串。还要确保ballotNum在前面有一个散列。
$(ballotNum + 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname);
$(ballotNum + 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname);