我有一个JS的小问题。我想让JS用AJAX改变一些div的类。工作正常,直到它应该找到div。JavaScript不会改变div
function filter(cb){
\t \t var value = cb.value;
\t \t if(cb.checked){ var state = "an" }
\t \t if(!cb.checked){ var state = "aus" }
\t \t var pstring = "Checkbox "+value+" = "+state;
\t \t var url = "shownitems.php?checkbox="+value;
\t \t $.get(url,
\t \t \t {
\t \t \t },
\t \t \t function(data){
\t \t \t \t var array = JSON.parse(data)
\t \t \t \t if(cb.checked){
\t \t \t \t \t array.forEach(function(value, index, array) {
//alert(value) works fine for every index
\t \t \t \t \t \t var divstring = value+"cont";
//alert(divstring) gives correct id. div "1cont" exists!
\t \t \t \t \t \t var div = document.getElementById(divstring);
\t \t \t \t \t \t alert(div); //null
\t \t \t \t \t });
\t \t \t \t }
\t \t \t \t if(!cb.checked){
\t \t \t \t \t array.forEach(function(value, index, array) {
\t \t \t \t \t \t var divstring = value+"cont";
\t \t \t \t \t \t var div = document.getElementById(divstring);
\t \t \t \t \t \t alert(div);
\t \t \t \t \t });
\t \t \t \t }
\t \t \t \t \t \t
\t \t \t }
\t \t);
\t }
摆脱你的单位报价为div成员。 – colecmc
不错的尝试,没有工作... –
当我使用你的代码,我得到一个'语法错误,无法识别的表达式''''' – colecmc