简体   繁体   English

如何在 javascript/jquery 中获得选中的复选框标题值?

[英]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'));

UPDATE更新

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);
    }
}

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.

相关问题 如何使用JavaScript / JQuery在Gridview中获取复选框检查值 - How to get checkbox checked value inside Gridview using JavaScript / JQuery javascript / jquery获取新选中的复选框的值 - javascript/jquery get value of newly checked checkbox 如何在jQuery中获取选中的复选框表值 - How to get checked checkbox table value in jquery 如何获取原版 JavaScript 中选中复选框的值? - How to get the value of checked checkbox in vanilla JavaScript? 如果选中复选框,我如何从复选框中获取属性值? - How can i get attribute value from a checkbox if it's checked? 如何在数组中获取选中的复选框和关联的文本框值,然后使用jquery或javascript将此值附加到url - How to get checked checkbox and associated text box value in an array and append this value to url using jquery or javascript 复选框组获取选中的复选框jquery的值 - checkbox group get value of checked checkbox jquery jQuery复选框-仅获取已选中复选框的值 - jquery checkbox - get the value of only the checked checkbox 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value 选中另一个复选框后,如何检查该复选框? [JavaScript的] - How can I check a checkbox when another checkbox is checked? [JavaScript]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM