admin管理员组文章数量:1301596
I have a check box and want to get the very inital value or in other words get the default value just like we get the default value of a text box as follows:
document.getElementById("myTextBoxID").defaultValue
I want to do the something similar either using jQuery/JS
document.getElementById("myChkBoxID").defaultValue
So if I get a checkbox value checked from the server the very first time...now whenever the user changes the checkbox ...i should have a way of restoring it back to the very inital state
Is it possible
I have a check box and want to get the very inital value or in other words get the default value just like we get the default value of a text box as follows:
document.getElementById("myTextBoxID").defaultValue
I want to do the something similar either using jQuery/JS
document.getElementById("myChkBoxID").defaultValue
So if I get a checkbox value checked from the server the very first time...now whenever the user changes the checkbox ...i should have a way of restoring it back to the very inital state
Is it possible
Share edited Oct 10, 2015 at 22:55 dsummersl 6,73752 silver badges67 bronze badges asked Jan 25, 2012 at 21:57 abbasabbas 4323 gold badges10 silver badges22 bronze badges4 Answers
Reset to default 4Try:
document.getElementById("myChkBoxID").defaultChecked
https://developer.mozilla/en/DOM/HTMLInputElement
Use the defaultChecked
property.
defaultChecked
and defaultValue
are defined in DOM level 2 as follows:
defaultChecked of type boolean
When type has the value "radio" or "checkbox", this represents the HTML checked attribute of the element. The value of this attribute does not change if the state of the corresponding form control, in an interactive user agent, changes. See the checked attribute definition in HTML 4.01.
defaultValue of type DOMString
When the type attribute of the element has the value "text", "file" or "password", this represents the HTML value attribute of the element. The value of this attribute does not change if the contents of the corresponding form control, in an interactive user agent, changes. See the value attribute definition in HTML 4.01.
Note that a checkbox does have a value attribute which you can change, but it is usually not something you would want, and can cause hard-to-find bugs...
Alternatives:
Since you are letting the server set the initial value, you can include a hidden input field and let the server set the initial state on it.
Or you can reset the entire form.
You'll need to store the default value somehow. In this instance, I would think a data attribute would be acceptable.
//onload
$('#textboxID, #checkboxID').each(function(){
$(this).data('default', $(this).val());
});
//on reset
$('#textboxID, #checkboxID').each(function(){
$(this).val( $(this).data('default'));
});
Just save the data when the page loads:
$(function() {
$(':input').each(function() {
$(this).data('default', $(this).val());
});
});
Now, for any <input />
element, you can get its default value easily:
$('#yourInputElement').data('default');
When you want to reset the element to its default state, just call val()
once more:
$('#yourInputElement').val($('#yourInputElement').data('default'));
本文标签: jquery javascript get checkbox39s initial valueStack Overflow
版权声明:本文标题:jquery javascript get checkbox's initial value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678284a2392013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论