[英]How can i get a checked Checkbox title value in javascript/jquery?
I want to get chekbox title value.我想获得 chekbox 标题值。 How can i do that ?
我怎样才能做到这一点 ?
<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" title='<%#Eval("CarServiceFormInvoiceRecordID") %>' />
My func doesn't work..我的功能不起作用..
function Checked(cb) {
if (cb.checked) {
alert(cb.title);
...
use this用这个
$('#chkSingle').attr('title')
to ensure that checkbox is checked确保选中复选框
$('#chkSingle[type="checkbox"]').filter(function(){return $(this).is(':checked')}).attr('tile')
jQuery: jQuery:
if ($(this).is(':checked')) {
alert($(this).attr('title'));
}
Javascript: Javascript:
change you call from Checked(this);
改变你从
Checked(this);
调用Checked(this);
to Checked(this.id);
Checked(this.id);
and you function to:你的功能是:
function Checked(checkId) {
var checkbox = document.getElementById(checkId);
if(checkbox.checked) {
alert(checkId);
....
Sorry, getting caught up with some of the comments here, I oversaw your actual problem.抱歉,在这里看到了一些评论,我监督了您的实际问题。 You need to have a look at the markup generated and see how the label for the checkbox is generated.
您需要查看生成的标记并查看复选框的标签是如何生成的。 Paste it here and I'll tell you how to access the value.
把它贴在这里,我会告诉你如何访问这个值。
Based on these comments:基于这些评论:
[NOX] - Can you show us the GENERATED code of your ?
[NOX] - 你能告诉我们你的 GENERATED 代码吗?
[Ahmet] -<input id="chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);">
[Ahmet] -
<input id="chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);">
The title
attribute is not generated in the output. title
属性不会在输出中生成。 Why?为什么? I don't know really.
我真的不知道。 Let's check it step by step.
让我们一步一步地检查它。
First, change your checkbox to this one:首先,将您的复选框更改为:
<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" title='HELLO' />
As you see, I changed the value of title
attribute.如您所见,我更改了
title
属性的值。 Check the output, if you haven't any title
attribute, try to change it to something like data-title
:检查输出,如果您没有任何
title
属性,请尝试将其更改为data-title
:
<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" data-title='HELLO' />
Now check your generated code, it must be something like this (I hope so):现在检查您生成的代码,它必须是这样的(我希望如此):
<input id="chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);" data-title="Hello">
If in generated code, the attribute data-title
is exists, then you succeed.如果在生成的代码中,属性
data-title
存在,那么你就成功了。 Now you must change your function to get this attribute:现在您必须更改您的函数以获取此属性:
alert(cb.getAttribute('data-title'));
As you comment, the generated code is:在您发表评论时,生成的代码是:
<span data-title="HELLO"><input id="ctl00_c1_RadGrid1_ctl00_ctl04_chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);" /></span>
So, the attribute you attach to the <asp:Checkbox />
became to an span
tag.因此,您附加到
<asp:Checkbox />
的属性变成了span
标记。 So you must change your function to something like this:所以你必须把你的函数改成这样:
function Checked(cb) {
var $input = $(cb);
var $span = $input.closest('span');
var title = $span.attr('data-title');
if ($input.is(':checked')) {
alert(title);
}
}
data-title
to title
attribute again.data-title
放回title
属性。you may try like this你可以试试这样
function Checked(cb) {
if (cb.checked) {
alert(cb.getAttribute('title'));
}
}
you may try like that ::你可以这样尝试::
$(':checked').prop('title', function(k) {
tit[k]= $(this).attr('title');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.