简体   繁体   中英

checkbox is checked if value is in database in php using jquery

i want to checkde the checkbox if its value is found in the database. for that i have two array one is checkbox array and another is result of database so, compare both and when match is found then checkde the checkbox.

 $('body').on('click','.update', function(){ var edit_id = $(this).attr('id'); var a=[]; var b=[]; $.ajax({ type: 'POST', url: 'update.php', data: 'edit_id='+edit_id, success: function(data) { var a=data.split(","); $("#up").val(a[0]); $("#title1").val(a[1]); $("#prod").val(a[2]); $("#dir").val(a[3]); $("#wri").val(a[4]); var c = (a[5]); var b=c.split("."); var g=document.getElementsByName("genre[]"); for(var i=0;i<g.length;i++) { var t=document.getElementsByName("genre[]")[i].value; document.getElementsByName("genre[]")[i].checked=false ; if(t[i]=b[i]) { document.getElementsByName("genre[]")[i].checked=true ; //$(t[i]).prop("checked", true); } } //rowElement.add("<tr>" + data + "</tr>"); //$('#chatbox table tr:last').after("<tr>" + data + "</tr>"); if (data = "true") { // $("#title1").val(data); alert("update "+data); } else { alert("not update"); alert("update"+data[0]); } } }); }); 

Your if condition is wrong

it should be if(t[i]===b[i])

You if condition works as a assignment . hence it will not work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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