Javascript jquery检查asp复选框是否被选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11477959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery check if asp checkbox is checked
提问by Laziale
Hello everyone I have a checkbox I want to check if its checked or no on client side, before postback happens. Here is the checkbox:
大家好,我有一个复选框,我想在回发发生之前检查它是否在客户端被选中。这是复选框:
<asp:CheckBox runat="server" CssClass="checkbox" Checked="true" ID="checkboxRules" />
Thanks in advance, Laziale
提前致谢, Laziale
UPDATE: The problem is that there is a button on the page too which is triggered by js code, and maybe I can add a line there to check if the checkbox is checked once the button is clicked, otherwise display message for unchecked checkbox:
更新:问题是页面上也有一个由 js 代码触发的按钮,也许我可以在那里添加一行以检查单击按钮后是否选中了复选框,否则显示未选中复选框的消息:
function enterClick() { $('#SUBMIT').click(); }
函数 enterClick() { $('#SUBMIT').click(); }
var ishown = false;
$(document).ready(function () {
$('#BUTTON').click(function (e) {
//I want to put the checkbox logic here
//我想把复选框逻辑放在这里
Thanks again
再次感谢
采纳答案by mccrager
Since it is a server-side Checkbox, it will send something like <input type="checkbox" class="checkbox" />
as HTML to the client after ASP.NET processes the control.
由于它是一个服务器端复选框,它会<input type="checkbox" class="checkbox" />
在 ASP.NET 处理控件后将类似HTML 的内容发送到客户端。
The id of the checkbox isn't going to be checkboxRules as you have it in the source code. ASP.NET will make a concatenation of the server-side form id + master page id (if using a master page) + checkboxRules so in this case I won't use a selector that depends on element id.
复选框的 id 不会像源代码中那样是 checkboxRules。ASP.NET 将连接服务器端表单 ID + 母版页 ID(如果使用母版页)+ checkboxRules,因此在这种情况下我不会使用依赖于元素 ID 的选择器。
We can make a jQuery selector as narrow as possible to only select inputs with a type of "checkbox" and with a CSS class of "checkbox".
我们可以使 jQuery 选择器尽可能窄,以仅选择具有“复选框”类型和 CSS 类“复选框”的输入。
$('input[type=checkbox] .checkbox').attr('checked')
will return the boolean value of the check status of the input. This will find any input on the page that is a checkbox with that CSS class.
将返回输入检查状态的布尔值。这将在页面上找到任何带有该 CSS 类的复选框的输入。
回答by Suave Nti
try...
尝试...
if ($('#<%= checkboxRules.ClientID %>').is(':checked')) {
...
}
回答by Anders Abel
Assuming your checkbox is the only item on the page having the checkbox class:
假设您的复选框是页面上唯一具有复选框类的项目:
var checked = $(".checkbox").is(':checked')
回答by end1dream
Add a data attribute to your checkbox and use the 'hasClass' function jQuery provides. Like below:
将数据属性添加到您的复选框并使用 jQuery 提供的“hasClass”函数。像下面这样:
var isChecked = $('[data-checkbox]').hasClass('checkbox-selected');
<asp:CheckBox data-checkbox="" Text="Some Caption" runat="server" />