将"checked"属性添加到jquery中的复选框 [英] Add "checked' property to checkbox in jquery

查看:45
本文介绍了将"checked"属性添加到jquery中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我实际上可以将复选框设置为选中状态没有问题,但是在当前情况下,我实际上需要将"checked"属性添加到复选框中,因为我正在使用的系统实际上是从DOM抓取HTML的./p>

我可以将复选框设置为选中状态,但是由于在再次插入HTML时没有可识别的选中"属性,因此它没有保存该属性,因此所有复选框都未选中:(

有没有一种方法可以添加和删除选中"作为属性,因此从原始HTML中可以明显看出选中了哪些复选框?例如

未选中的框

<input type="checkbox" value="yes" />

选中框

<input type="checkbox" checked value="yes" />

感谢任何帮助!

解决方案

我正在从页面中提取RAW html,但是默认情况下,似乎选中的复选框没有任何标识属性或属性,因此提取的HTML不知道是否选中了它们,是否可以添加或删除选中的属性即可解决我的问题.

是的,您从innerHTML获取的元素标记中没有反映出选中的状态.

您可以通过显式设置和删除元素上的属性来强制使用该属性.以下是单击复选框时执行此操作的示例,或者您也可以通过在获取HTML之前立即更新元素来执行此操作.

例如,它可在Chrome,Firefox,IE11和IE8上运行:

 $("input").on("click", function() {
  if (this.checked) {
      this.setAttribute("checked", "checked");
  } else {
      this.removeAttribute("checked");
  }
  snippet.log(this.parentNode.innerHTML);
}); 

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input type="checkbox" value="yes">
</div>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

请注意,由于jQuery在attr中对checked进行了特殊处理,因此我不得不直接去DOM.

So I have no problem actually setting checkboxes as checked, however in my current case I need to actually add the "checked" property to the checkboxes, as the system I'm working in literally grabs the HTML from the DOM.

I can set the checkbox to be checked, however as there's no identifying "checked" property when the HTML gets inserted again it doesn't hold the property so all checkboxes come back unchecked :(

is there a way to add and remove 'checked' as a property so it will be apparent from the raw HTML what boxes are checked? e.g.

unchecked box

<input type="checkbox" value="yes" />

checked box

<input type="checkbox" checked value="yes" />

Appreciate any help!!

解决方案

I'm pulling RAW html from the page, but by default it seems checked checkboxes don't have any identifying properties or attributes so the HTML that is extracted doesn't know if they were checked or not, if I can add or remove the checked property it will solve my problem.

That's correct, the checked state isn't reflected in the markup you get back from innerHTML for the element.

You can force it to be by explicitly setting and removing the attribute on the element; below is an example doing it when the checkbox is clicked, or alternately you might do it by updating the elements immediately before grabbing their HTML.

This works on Chrome, Firefox, IE11, and IE8, for instance:

$("input").on("click", function() {
  if (this.checked) {
      this.setAttribute("checked", "checked");
  } else {
      this.removeAttribute("checked");
  }
  snippet.log(this.parentNode.innerHTML);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input type="checkbox" value="yes">
</div>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Note I had to go straight to the DOM to do it, as jQuery has special handling in attr for checked.

这篇关于将"checked"属性添加到jquery中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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