简体   繁体   English

试图使用函数用Javascript动态填充HTML表

[英]trying to use a function to populate a HTML table dynamically with Javascript

I have a table that populates fine with javascript and Ajax, but now I need to add in checkboxes and have them show checked if saved in the database that way (1 for checked 0 for not). 我有一个用javascript和Ajax填充的表,但是现在我需要添加复选框,并让它们显示是否以这种方式保存在数据库中(1表示选中,0表示不选中)。 Instead of writing 而不是写作

 var aChecked='';
 aChecked += '<input type="checkbox" id="amS"'+(data.amS == 1 ? 'checked="checked"':'')+'/>';

for every checkbox ( there are about 24 of them) I am trying to use a modified function that I use to see if other checkboxes not in the table are checked. 对于每个复选框(其中大约有24个),我试图使用修改后的函数,该函数用于查看是否选中了表格中未存在的其他复选框。 The function is: 该函数是:

function Checked(){
this.update();
}

is above my $.ajax and this is in my success(function(data) portion. 在$ .ajax之上,这在我的success(function(data))部分中。

Checked.prototype.list = ['am','fol','tich','book','neb','ster','byte','ing'];
Checked.prototype.update = function(){
for( var i = 0 ; i < this.list.length ; i++ ){
var check = this.list[i];
this[ check + 'S' ] += '<input type="checkbox" id="'+this[ check + 'S' ]+'"'+(data.this[ check + 'S' ] == 1 ? 'checked="checked"':'')+'/>';//$("#" + check ).is(":checked") ? 1 : 0;
                      }// end of for loop  
                    }
                    var checks = new Checked();

and then I can reference it in my append statement like so 然后我可以像这样在我的append语句中引用它

<td>'+checks.amS+'</td>

Problem is I am getting this error when I try to run it: 问题是当我尝试运行它时出现此错误:

TypeError: data.this is undefined

I am not sure how i need to write this in order for this to work and not have to write it out the long way and violate the DRY principle. 我不确定我该如何写才能使它起作用,而不必写得太长并违反DRY原理。 I have tried 我努力了

data.(this[ check + 'S' ])

and

data.[this[ check + 'S' ]]

both of which throw syntax errors. 两者都引发语法错误。 Anyone know how I can accomplish this? 有人知道我该怎么做吗? Thanks 谢谢

Change 更改

this[ check + 'S' ] += '<input type="checkbox" ... >'  
// this[ check + 'S' ] = this[ check + 'S' ] + '<input type="checkbox" ... >'
// this[ check + 'S' ] = undefined + '<input type="checkbox" ...  >'

to

this[ check + 'S' ] = '<input type="checkbox" ... >'

Check your code plz 检查您的代码plz

this[ check + 'S' ] =
    '<input type="checkbox" id="'+this[ check + 'S' ]+'"' // this[ check + 'S' ] is undefined
     +(data.this[ check + 'S' ] == 1 ? 'checked="checked"':'')+'/>'; // this[ check + 'S' ] is undefined
     //$("#" + check ).is(":checked") ? 1 : 0;

to

this[ check + 'S' ] =
    '<input type="checkbox" id="'+check + 'S'+'"' // modified
     +(data[ check + 'S' ] == 1 ? 'checked="checked"':'')+'/>'; // modified
     //$("#" + check ).is(":checked") ? 1 : 0;

"this" means "var checks" on your update function in this case 在这种情况下,“ this”表示对更新功能进行“ var check”

"data[this]" is really wrong access, “数据[此]”确实是错误的访问,

why this means object(instance) not string(object's key) 为什么这意味着对象(实例)不是字符串(对象的键)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM