I want to get chekbox title value. 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);
...
I want to get chekbox title value. 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);
...
Share
asked May 26, 2013 at 15:25
ErdoganErdogan
1,01215 silver badges27 bronze badges
19
-
2
use
cb.getAttribute('title')
– Tamil Selvan C Commented May 26, 2013 at 15:26 - @TamilSelvan How is this different from OP using cb.title? – A. Wolff Commented May 26, 2013 at 15:31
- @Ted How is this different from OP using cb.checked? – A. Wolff Commented May 26, 2013 at 15:31
- "My func doesn't work." Which means? Any error or what? – A. Wolff Commented May 26, 2013 at 15:31
-
1
Maybe
<asp:Checkbox />
will change thetitle
attribute to<label />
tag. – user1823761 Commented May 26, 2013 at 15:35
5 Answers
Reset to default 2use this
$('#chkSingle').attr('title')
to ensure that checkbox is checked
$('#chkSingle[type="checkbox"]').filter(function(){return $(this).is(':checked')}).attr('tile')
Based on these ments:
[NOX] - Can you show us the GENERATED code of your ?
[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. 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. Check the output, if you haven't any title
attribute, try to change it to something like 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.
Now you must change your function to get this attribute:
alert(cb.getAttribute('data-title'));
UPDATE
As you ment, 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. 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);
}
}
- Check the Working jsFiddle Demo.
- I think you can put back
data-title
totitle
attribute again.
jQuery:
if ($(this).is(':checked')) {
alert($(this).attr('title'));
}
Javascript:
change you call from Checked(this);
to 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 ments 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.
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');
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744855657a4597386.html
评论列表(0条)