[英]How to get Value of CheckBox in Javascript if its already Checked?
If a Check Box is Checked or Unchecked and we stored this value in Database, then How can we get that Value in JavaScript? 如果复选框已选中或未选中,并且我们将该值存储在数据库中,那么我们如何在JavaScript中获取该值?
Here is my code: 这是我的代码:
<div class="editor-label" style="width: 110px;">
<%: Html.LabelForEx(model => model.SecurityVulnerability.SecurityVulnerability) %>
</div>
<div class="editor-field" style="width: 60px; padding-top: 0;">
<%: Html.CheckBoxFor(x => x.SecurityVulnerability.SecurityVulnerability) %>
</div>
Now If I loaded the page then check Box might be checked or not. 现在,如果我加载了页面,则可能会选中“复选框”。 If it is checked then I want to get this value as true(or whatever) or if not checked then false in JavaScript(I am writing some javaScript Function). 如果选中,则我想将此值设为true(或其他值),或者如果未选中,则在JavaScript中为false(我正在编写一些javaScript函数)。 I tried using onclick function but that only work when user do check and uncheck in UI Manually. 我尝试使用onclick函数,但仅在用户手动检查和取消检查UI时才起作用。 Also if User clicks on UI manually then also I want to get value accordingly. 另外,如果用户手动单击UI,那么我也要相应地获取价值。
I am quite new to javaScript and razor. 我对javaScript和razor很陌生。
When page is fully loaded, it iterates through all checkboxes and logs it's check state. 页面完全加载后,将遍历所有复选框并记录其检查状态。
window.onload = function() { var checkBoxes = document.querySelectorAll('input[type="checkbox"]'); for(var i = 0; i < checkBoxes.length; i++) { console.log(checkBoxes[i].checked); } }
<label>I'm checked <input type="checkbox" value="foobar" checked></label><br> <label>I'm not checked <input type="checkbox"></label>
If you have only one unique input (with ID) then you can do that. 如果只有一个唯一输入(带有ID),则可以这样做。
window.onload = function() { var checkBox = document.getElementById("SecurityVulnerability_SecurityVulnerability"); console.log(checkBox.checked); }
<label> Checked<input type="checkbox" id="SecurityVulnerability_SecurityVulnerability" checked></label>
You can just read whether it is checked
or not. 您可以读取是否checked
它。
var checkbox = document.getElementById('checkbox');
alert(checkbox.checked);
https://jsfiddle.net/sLx9926o/ https://jsfiddle.net/sLx9926o/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.