我正在尝试改进一些我写入的将元素推入数组的元素,如果它们以前不在数组中。然后,我希望该数组显示在选择字段中。我有这个所有的JavaScript工作,但认为这将是一个有趣的尝试,我自己尝试在jQuery中做同样的事情。我想我接近,但由于某种原因,在我的浏览器控制台给我一个错误如下:数组按下按钮点击jquery
"[Error] SyntaxError: Cannot use the keyword 'var' as a variable name. (anonymous function)"
这里是我到目前为止的代码。我已经包含了(以此顺序)我尝试在jQuery,然后我的工作JavaScript代码(注释掉),以及我的功能更新下拉列表。我最终会尝试在jQuery中重新创建该函数。任何人都可以告诉我为什么我得到这个错误,或者我在我的jQuery做错了什么?谢谢。
var clientArray = [];
var clientInput = document.getElementById("newClt");
var sel = document.getElementById("cltList");
$("#addCltBtn").click(function(){
var found = $.inArray(clientInput, clientArray) > -1;
if(found >= 0) {
//Element was found update errorMessage div
$(errorMessage).html("This Client Already Exists");
} else{
clientArray.push(clientInput.value);
}
});
//
// document.getElementById("addCltBtn").onclick=function(){
//
// console.log(clientArray.length);
// if (!contains(clientArray,clientInput.value)) {
// clientArray.push(clientInput.value);
// console.log("Objects: " + clientArray.join(", "));
// updateDropList(clientInput.value);
// }
// else alert("You already have a client by that name!");
// }
function updateDropList(value){
var opt = document.createElement('option');
opt.innerHTML = value;
opt.value = value;
sel.appendChild(opt);
}
function contains(a, obj){
for(var i =0; i< a.length; i++){
if(a[i] === obj){
return true;
}
}
return false;
}
发布所有相关代码\ HTML标记问题本身 –
何时发生此错误?它指向哪条线?你已经包含的代码示例似乎没问题。 – Pavlo
它指向我的代码中的第65行var found = $ .inArray(clientInput,clientArray)> -1; –