jQuery - 在使用remoteAttr()清除复选框后重新选中复选框。 [英] jQuery - recheck checkboxes after clearing them with remoteAttr()

查看:203
本文介绍了jQuery - 在使用remoteAttr()清除复选框后重新选中复选框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我重置一堆输入:复选框字段与resetAttr(),这是完美的,我无法重新检查这些复选框使用ajax响应的数据结合.attr('checked',' true')。



所以脚本的工作流应该是这样:


  1. 在ajax请求期间获取一些数据。


  2. 打开表单时,请重置其所有内容,包括复选框。这不是
    document.getElementById(ID)。reset(); 单独使用。
    因此我使用 removeAttr();


  3. 请求并将其放入DOM使用 jQuery('#SomeID')。find('input [value ='+ SomeObject [key] +']')。attr('checked' true');


没有使用remoteAttr如步骤3中所述。使用removeAttr()导致结果,重填复选框将被忽略。



有任何建议吗?
最好的问候
Ralf



更新:我发现一个解决方案,这是一个有点讨厌,但工作:



首先我删除选中的属性:
jQuery('#form')。find('input:checkbox')。attr('checked',false);



然后使用以下命令重新设置表单:
document.getElementById(such-sets之后,我使用jQuery()重新填充表单选中的复选框,文本框等,然后单击确定。 .val()等。

解决方案


似乎是因为attr函数不更新复选框的状态。



请尝试更改:
$('。someClass')。attr(checked,true);



into
$('。someClass')。prop(checked,true);



详情请参阅官方文档:
http://api.jquery.com/prop/


if I reset a bunch of input:checkbox fields with resetAttr(), which is working perfectly, I'm unable to recheck those checkboxes using the data of an ajax-response in combination with .attr('checked','true').

So the workflow of the script should be like this:

  1. Get some data during an ajax-request. If this is successfull, continue.

  2. When opening the form, reset all of it's content, including the checkboxes. That isn't working with document.getElementById("ID").reset(); alone. Therefore I used removeAttr();

  3. Now take the data of the ajax-request and put it into the DOM using jQuery('#SomeID').find('input[value="'+SomeObject[key]+'"]').attr('checked','true');

Without having used remoteAttr() before the form will be filled as described in Step 3. Using removeAttr() leads to the result, that refilling the checkboxes will be ignored.

Any suggestions? Best regards Ralf

UPDATE: I found a solution, which is a little nasty but working:

First I remove the checked attributes with: jQuery('#form').find('input:checkbox').attr('checked',false);

And then reset the form once again, using: document.getElementById("such-sets-_viewSuchSets-form").reset();

After that I refill the form with checked checkboxes, textboxes etc. using jQuery().val() etc.

解决方案

For whom who are searching solution for this problem. Seems it is because of the "attr" function does not update the status of checkbox.

Please try to change: $('.someClass').attr("checked", true);

into $('.someClass').prop("checked", true);

For detail please visit the offical document: http://api.jquery.com/prop/

这篇关于jQuery - 在使用remoteAttr()清除复选框后重新选中复选框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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