为什么不设置.attr('checked','checked')? [英] Why won't .attr('checked','checked') set?

查看:587
本文介绍了为什么不设置.attr('checked','checked')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段(我使用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();
});

假设邮件正确工作,并返回正确的预构建 tr> 与一些< td> 这是奇怪的: $(result).find()行找到正确的输入(这是一个复选框,因为它是单元格中唯一的输入) click()功能正确,但它拒绝将框设置为选中,我需要发生。



这是一个疯狂的转折,当我得到超级具体和更改 $(result).find()行到此(复选框的id):

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

它检查框,但不运行 / code> function!如果我将它设置为

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

它运行点击功能,就像检查框,但框保持未选中, do

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

它什么都不做。



世界上可能有什么事情呢?



编辑>

以下是我在 $(function())调用中设置点击事件的方法:

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

它适用于所有其他复选框,并且新添加的复选框确实可以工作,如果点击



编辑2



经过一番挖掘,我发现了:

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

实际上 调用我的点击功能。但是,当调用点击功能(我已经检查,以确保输入是正确的复选框,它是),

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

日志返回false,即使框被实际检查! GAH WTF

解决方案

我想出来,谢谢所有把我放在正确轨道上的人。这里是我要做的:



首先,不要接线我的复选框运行 click()更新它们以在 change()上运行。在我这样做后,我使用以下行代替另一个:

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

复选框保持选中状态,并调用正确的函数。



再次感谢大家!


I have the following snippet of code (I'm 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();
});

Assume that the post works correctly and returns a correct pre-built <tr> with some <td>s. Here's the weirdness: the $(result).find() line finds the correct input (which is a checkbox, as it's the only input in the cell) and runs the chained click() function correctly, but it REFUSES to set the box as checked, which I need to happen.

Here's a crazy twist, too... when I get super specific and change the $(result).find() line to this (the id of the checkbox):

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

It checks the box, but doesn't run the click() function! If I set it to

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

it runs the click function as though the box were checked, but the box remains unchecked, and if I do

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

it does nothing at all.

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

Thanks!

EDIT

Here's how I set my click event in the $(function()) call:

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

It works as is for all the other checkboxes, and the newly-added checkbox does indeed work if clicked by hand.

EDIT 2

After some digging I discovered that this:

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

actually does call my click function in addition to checking the box. However, when the click function is called (I've already checked to make sure that input is the correct checkbox and it is),

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

the log returns false, even though the box is actually checked! GAH WTF

解决方案

I figured it out, thank you to all who put me on the right track. Here's what I had to do:

First, rather than wiring my checkboxes to run on click(), I updated them to run on change(). After I did this, I used the following line in place of the other:

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

and everything works now! The checkbox stays checked and the correct function is called.

Thanks again everyone!

这篇关于为什么不设置.attr('checked','checked')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆