删除无法与MooTools一起使用的类 [英] Remove class not working with MooTools
问题描述
我在网页中使用以下代码,根据单选按钮的选择来更改选择元素的类.
I'm using the following piece of code in my webpage to change the class of select elements depending on the choice of a radio button.
我添加课程的那一部分工作正常,但是其他(我删除它们的那一部分)却无法正常工作.在错误控制台中我没有收到任何错误消息,并且当我更改代码以使删除该类的部分将另一个类放到select元素中时,它运行良好.
The part where I add the class works fine but the other (where I remove them) doesn't work. I get no error in the Error console and when I changed my code to have the part that removes the class put another class to the select elements it worked fine.
<script type="text/javascript">
window.addEvent('domready', function(){
$('votconj').addEvent('click', function() {
// This works fine
$('first_name_conjoint').addClass("validate['required','nodigit']");
$('last_name_conjoint').addClass("validate['required','nodigit']");
$('jj_conjoint').addClass("validate['required']");
$('mm_conjoint').addClass("validate['required']");
$('aaaa_conjoint').addClass("validate['required']");
$('conjoint_regime').addClass("validate['required']");
new FormCheck('formulaire');
});
$('votconj_no').addEvent('click', function() {
// This doesn't work !
$('first_name_conjoint').removeClass("validate['required','nodigit']");
$('last_name_conjoint').removeClass("validate['required','nodigit']");
$('jj_conjoint').removeClass("validate['required']");
$('mm_conjoint').removeClass("validate['required']");
$('aaaa_conjoint').removeClass("validate['required']");
$('conjoint_regime').removeClass("validate['required']");
new FormCheck('formulaire');
});
new FormCheck('formulaire');
});
</script>
// The radio button
<label>Conjoint :</label>
<input type="radio" name="votconj" id="votconj" value="oui">oui
<input type="radio" name="votconj" id="votconj_no" value="non" checked="checked">non
推荐答案
它不起作用,因为MooTools的".removeClass()"方法将类名简单地塞入了regex的中间,而又不致于逃避嵌入正则表达式元字符.
It doesn't work because the MooTools ".removeClass()" method simple-mindedly jams the class name into the middle of a regex without bothering to escape embedded regex meta-characters.
但是,您可以通过适当地引用自己的方式来解决此问题.在此示例中,它看起来像这样:
You can, however, work around the problem by doing the appropriate quoting yourself. In this example, it'd look like this:
$('first_name_conjoint').removeClass("validate\\['required','nodigit'\\]");
此处是jsfiddle.
Here is a jsfiddle.
这篇关于删除无法与MooTools一起使用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!