本文介绍了我可以通过jQuery的ID的最后部分访问控件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态创建的控件,我给了IDckbxEmp



正如我发现查看源,Sharepoint(或Chrome或某人 )无庸置疑地重新命名(并将其命名为for)来控制,给予威尔士式的ctl00_ctl24_g_1caf4ad1_092d_4224_bd10_28ab028aab50_ctl00_ckbxEmp



所以(我假设这是原因),所有尝试通过jQuery操纵ckbxEmp失败 - 也就是说,当我查找我给它的短ID,如下所示:



$($)$(文件).on(change,'#ckbxEmp',function(){
console.log('Function has been reached');
if($(this).is(:checked)){
alert(Checked);
} else {
alert(Not checked);
}
});

所以我可以将代码更改为:



($)$($)$($)$(文件).on(change,'#ctl00_ctl24_g_1caf4ad1_092d_4224_bd10_28ab028aab50_ctl00_ckbxEmp',function(){
console.log b $ b if($(this).is(:checked)){
alert(Checked);
} else {
alert(Not checked);
}
});

...这可以是一次性的,但是我有很多动态创建控制我想要操纵,我认为他们都将被威胁,这将是一个痛苦。



-OR,希望有一种方式寻找不完全但以结尾的ID匹配。那可能吗?如果是这样,那么语法是什么?



注意:这是对。



更新



奇怪的是(这开始变得非常刺激),这不行:

  $(document).on(change,'#ctl00_ctl24_g_1caf4ad1_092d_4224_bd10_28ab028aab50_ctl00_ckbxEmp',function(){
console.log('Function has been reached');
如果($(this).is(:checked)){
alert(Checked);
} else {
alert(Not checked);
}
});



更新2



Robert McKee的回答,加上这是后续行动的问题,导致了一个有效的解决方案:

  $(文件).on(change,'[id $ = ckbxEmp]',function(){
alert('Function has been reached');
if($(this).is :check)){
alert(Checked);
} else {
alert(Not checked);
}
});

这是这个,这不是别的, em> - 鹿猎人

解决方案
  $(document) ,'[id $ = ckbxEmp]',function(){


I have a dynamically-created control which I gave the ID "ckbxEmp"

As I discovered by "Viewing Source," Sharepoint (or Chrome, or "somebody") impertinently and presumptuously re-Ids (and names it, and "for"s it) this control, giving it the Welsh-esque "ctl00_ctl24_g_1caf4ad1_092d_4224_bd10_28ab028aab50_ctl00_ckbxEmp"

And so (I assume this is the reason), all attempts to manipulate "ckbxEmp" via jQuery fail - that is to say, when I look for the short ID I gave it, like so:

$(document).on("change", '#ckbxEmp', function () {
    console.log('Function has been reached');
    if ($(this).is(":checked")) { 
        alert("Checked");
    } else {
        alert("Not checked");
    }
}); 

So I can either change that code to this:

$(document).on("change", '#ctl00_ctl24_g_1caf4ad1_092d_4224_bd10_28ab028aab50_ctl00_ckbxEmp', function () {
    console.log('Function has been reached');
    if ($(this).is(":checked")) { 
        alert("Checked");
    } else {
        alert("Not checked");
    }
}); 

...which would be okay as a one-time thing, but I have many dynamically-created controls that I will want to manipulate, and I assume they will all be Welshified, and this will be a bit of a pain.

-OR, hopefully there is a way to look for ID matches that are not exact but "ends with". Is that possible? If so, what is the syntax for that?

Note: This is a followup to this question.

UPDATE

Bizarrely enough (this is starting to get really irritating), this doesn't work, either:

$(document).on("change", '#ctl00_ctl24_g_1caf4ad1_092d_4224_bd10_28ab028aab50_ctl00_ckbxEmp', function () {
    console.log('Function has been reached');
    if ($(this).is(":checked")) {
        alert("Checked");
    } else {
        alert("Not checked");
    }
}); 

UPDATE 2

Robert McKee's answer, plus the one from the question to which this is a follow-up, have led to a working solution, codely:

$(document).on("change", '[id$=ckbxEmp]', function () {
    alert('Function has been reached');
    if ($(this).is(":checked")) {
        alert("Checked");
    } else {
        alert("Not checked");
    }
}); 

"This is this. This ain't something else. This is this." - the Deer Hunter

解决方案
$(document).on("change", '[id$=ckbxEmp]', function () {

这篇关于我可以通过jQuery的ID的最后部分访问控件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:13