Why won't the .attr ('checked', 'checked') parameter be set?

I have the following code snippet (I am using jQuery 1.4.2):

$.post('/Ads/GetAdStatsRow/', { 'ad_id': id }, function(result) { $('#edit_ads_form tbody').prepend(result); $(result).find('td.select-ad input').attr('checked','checked').click(); }); 

Suppose mail works correctly and returns the correct pre-built <tr> with some <td> s. Here's the weird $(result).find() : the line $(result).find() finds the correct input (which is the checkbox, since it is the only input in the cell) and correctly executes the click() chain, but it REFUSES to set this field as checked, which I should happen.

Here's a crazy twist too ... when I get a superspecial value and change the line $(result).find() to this (checkbox identifier):

 $('#ad_' + id).click(); 

It checks the checkbox, but does not execute the click() function! If I set it to

 $('#ad_' + id).attr('checked','checked').click(); 

it starts the click function as if the checkbox were checked, but the window remains unchecked and if I do

 $('#ad_' + id).click().attr('checked','checked'); 

he does not do anything.

What could be in the world? I'm running out of hair ...

Thanks!

EDIT

Here, as I set the click event in the call to $(function()) :

 $('td.select-ad input').live('click',AdPreview.selectAd); 

It works the same as for all other flags, and the added flag really works if you click manually.

EDIT 2

After some digging, I found that this:

 $('#ad_' + id).click(); 

actually calls the click function in addition to checking the field. However, when the click function is called (I already checked that input was the correct checkbox, and it is)

 var input = $(this); console.log(input.is(':checked')); 

the log returns false, even if the field is really checked! GAH WTF

+4
source share
5 answers

I realized this, thanks to everyone who set me on the right path. Here is what I had to do:

First, instead of connecting my checkboxes to run on click() , I updated them to run on change() . After that, I used the following line instead of another:

 $('#ad_' + id).click().change(); 

and everything works now! The checkbox remains set and the correct function is called.

Thanks again everyone!

+1
source

I think this is because you are doing your find on some html that is not in the DOM.

 $.post('/Ads/GetAdStatsRow/', { 'ad_id': id }, function(result) { // the "result" html is copied into the DOM $('#edit_ads_form tbody').prepend(result); // now you're doing a find on some HTML which isn't in the DOM. $(result).find('td.select-ad input').attr('checked','checked').click(); }); 

This explains why a particular material works to a certain extent. Here are individual issues:

 $('#ad_' + id).click(); 

A window will open (to check it), but no events have been added to it.

 $('#ad_' + id).attr('checked','checked').click(); 

This sets the attribute to validate, and then clicks it, undoing it immediately.

+4
source

http://www.electrictoolbox.com/check-uncheck-checkbox-jquery/

jQuery (or the DOM browser? I forgot) expect a true or false value for the attribute "checked", despite the fact that in HTML-source-land it is "checked =" checked ".

NTN.

+2
source

I suspect that what is happening here is that Click actually disables this checkbox. This is because the Find method, as you say, returns a checkbox.

Try to delete the call.

Here is a sample code

 <html> <head> <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script> </head> <body> <script type="text/javascript"> function clicked() { console.log('clicked'); } $(document).ready(function() { var result = '<tr><td><input type="checkbox" id="chkbx" onclick="clicked()" /></td></tr>'; $('#edit_ads_form tbody').prepend(result); $('#edit_ads_form tbody').find("input").click(); }); </script> <table id="edit_ads_form"> <tbody> </tbody> </table> </body> </html> 
+1
source

Does not execute $(result).find('td.select-ad input').attr('checked','checked').click(); to check it ( .attr('checked','checked') ) and then remove it by clicking ( .click() )?

Forgive me if something is missing me, but I'm sure it is. This is why $('#ad_' + id).click(); works, but your other examples don't.

0
source

Source: https://habr.com/ru/post/905576/


All Articles