Tuesday 15 June 2010

jquery - Automatically set a checkbox replacing div to "checked" -


I have a function that creates internal devices for parent devices, it receives 2 parameters: "div_id" and The "visitor_entered" function also puts a checkbox in one of these designs and replaces it with a div with a background image. When I click on div - it changes the background image, then I do this:

  $ ("# checkbox_" + div_id) .each (function () {$ (this ) .hide (). Later ("& lt; div class = 'Class_checkbox' id = 'visitor_entered _" + div_id + "' /" gt; ");}); $ ("# Visitor_entered _" + div_id) .on (. $ (This 'click', function () {$ (this) .toggleClass ('check') Previous (). Sahara ('check',) .is ( 'Checked'));});   

Now what I want, when this function runs, to see that "visitor_entered" is equal to 1, and if so-- "# checkbox _" + div_id div To "check" I tried to do this:

  if (visitor_entered == 1) {warning ("1, true!"); $ ("# Visitor_entered _" + div_id) .prop ('checked', $ (this) .is () 'checked'.); }   

It was alert when needed, but div to check is not set, how can I do this?

** edit **

whole function:

  function create_inner_divs (div_id , Visitor_entered) {for (var i = 0; i & lt; 6; i ++) {divs_array [i] = div_id * 10 + i; Var div_width = 704/6; $ ("& Lt; div id = '' + divs_array [i] +" 'style =' width: "+ div_width +" pixel; float: ok, word-wrap: normal; word-break: break-all; Padding: 0 2px 0 2px; 'gt; & amp; nbsp; & lt; / div & gt; "). Attachments (" # div_visitor _ "+ div_id);} $ (" & lt; div id =' div_visitor_entered _ "+ Div_id + 'class =' ​​div_visitor_entered '& gt; & lt; input name =' checkbox_visitor_entered 'type =' checkbox 'id =' checkbox_" + div_id + '' class = 'input_class_checkbox' & gt; & Lt; / div & gt; "). AppendTo (" # div_visitor _ "+ div_id); $ (" & lt; div id = 'div_print_voucher_icon' class = 'div_print_voucher_icon' 'gt; & lt; / div & gt; "AppendTo" ("# div_visitor _" + div_id); $ ("div id = 'delete_visitor' class = 'delete_visitor'"> 
) "appendTo (" # div_visitor _ "+ Div_id); $ (" # Checkbox_ "+ div_id) .Each (function () followed by {$ (this) .hide () (" Lt; div class = 'class_checkbox' id = 'visitor_entered _ "+ div_id +' '/ & gt; ");}); $ ("# Visitor_entered _" + div_id) .on (. $ (This 'click', function () {$ (this) .toggleClass ('check') Previous (). Sahara ('check',) .is ( 'Checked'));}); If (visitor_entered == 1) {Warning ("1, true!"); $ ("# Visitor_entered _" + div_id) .prop ('checked', $ (this) .is () 'checked'.); }}

Ah yes! I think I have it!

  if (visitor_entered == 1) {warning ("1, true!"); // Here, you are trying to set a "check" property ... on a DIV // Because of that, you are unable to find anything, because there is no such property which is a div unit / / The only reason that your click event handler seems to work in the code is that you are toggling the class on the div at the same time / at the same time you have property $ ("# visitor_entered_" + div_id) Trying to change) .prop ('check', $ (this) .is ('checked')); }   

You need to manage the states of both elements ... do something like, instead:

  if (visitor_entered == 1) {Alert ("1, true!"); Var veDiv = $ ("# visitor_entered_" + div_id); // First, toggle the square on the related checkbox: veDiv.toggleClass ('check'); // Second, find the Brother checkbox var vechk = veDiv.prev ('[type = checkbox]'); // Now set that checkbox checked property ... which does not accept // boolean value ... this is either empty // (unchecked) or 'check' veChk.prop (' Check ', (veChk. (': Check '))' ':' check '); }   

(sorry ... copy and paste from your code will not fix this problem)

One of the downsides to make this much more channing It is easy to believe that you are killing the right things on every part of the series. When you start the problems, break it so that you can test each perception separately. I probably used to manage your on-click in such a way:

  $ ("# visitor_entered_" + div_id) .on ('click', function () {// you already have One reference is the clicked div, so get my // instantly checkbox @ myChk = $ (this) .prev ('[type = checkbox]'); // toggle div's square $ (this) .toggleClass ('check '); / / And then toggle the checked property myChk.prop (' check ', (veChk.is (': checked '))' ':' check ');});   

(Your onclic code had to be applied to the same fix as the code above ...)

Good luck!

No comments:

Post a Comment