Вопрос

I want to use prop() to set the attribute of a checkbox to checked.

When I use prop("checked",true) I can visually see that the checkbox is checked, but if I look at the HTML there is no attribute value called checked.

For example:

<input type="checkbox" class="my_class" checked>

is what I would expect to see after using prop("checked", true). However, I don't see any change in the HTML code. I want to later reference whether or not the attribute checked is set, but since there is not attribute called checked in the HTML, then I'm unable to reference it.

Это было полезно?

Решение

When using prop() you are changing the property and not the attribute, so the changes can't be seen in a DOM inspector, but it is for all intents and purposes changed.

To later see if the element is checked, you'd do:

$('.my_class').is(':checked')

which would return true when checked or false when unchecked, regardless of what you might see in a DOM inspector.

Другие советы

Is there a specific reason you wouldn't use attr() for this application?

If not, that's what you should be using instead of prop()

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top