输入值上的 jQuery 切换类
我想根据文本输入的值向输入添加类。 我的问题是输入呈现所有类。 这是 html 部分:
<form name="imcForm" id="imc">
<p>poids: <input type="text" name="poids" size="10"></p>
<p>taille: <input type="text" name="taille" size="10"></p>
<p><input type="button" id="calcul" value="Calculer" onClick="calculImc()"></p>
<p>result : <input type="text" name="imc" size="10" id="imcResult"></p>
<p>Interprétation: <input type="text" name="interpretation" size="25" id="interpretation"></p>
</form>
这是 javascript/jQuery
$('#calcul').on("click", function() {
$("#interpretation:input[value=itemA]").toggleClass('a');
$("#interpretation:input[value=itemB]").toggleClass('b');
$("#interpretation:input[value=itemC]").toggleClass('c');
$("#interpretation:input[value=itemD]").toggleClass('d');
$("#interpretation:input[value=itemE]").toggleClass('e');
$("#interpretation:input[value=itemF]").toggleClass('f');
$("#interpretation:input[value=itemG]").toggleClass('g');
});
另外,看看渲染输入的内容:
<input type="text" id="interpretation" size="25" name="interpretation" class="denutrition normale moderee severe morbide">
那么为什么每个元素在单击时都会收到每个类?
I want to add a class to an input depending on the value of the text input.
My problem is that the input renders all the classes.
Here is the html part :
<form name="imcForm" id="imc">
<p>poids: <input type="text" name="poids" size="10"></p>
<p>taille: <input type="text" name="taille" size="10"></p>
<p><input type="button" id="calcul" value="Calculer" onClick="calculImc()"></p>
<p>result : <input type="text" name="imc" size="10" id="imcResult"></p>
<p>Interprétation: <input type="text" name="interpretation" size="25" id="interpretation"></p>
</form>
And here is the javascript/jQuery
$('#calcul').on("click", function() {
$("#interpretation:input[value=itemA]").toggleClass('a');
$("#interpretation:input[value=itemB]").toggleClass('b');
$("#interpretation:input[value=itemC]").toggleClass('c');
$("#interpretation:input[value=itemD]").toggleClass('d');
$("#interpretation:input[value=itemE]").toggleClass('e');
$("#interpretation:input[value=itemF]").toggleClass('f');
$("#interpretation:input[value=itemG]").toggleClass('g');
});
Also, look at what render the input :
<input type="text" id="interpretation" size="25" name="interpretation" class="denutrition normale moderee severe morbide">
So why does each element recieve each class on click?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在
此外,您还可以大大简化您的代码,特别是如果类名始终是输入字段中的最后一个字母。
You're missing a
>
at the end of your<button>
input.Also you can greatly simplify the code you've got, especially if the class name is always the last letter of what's in the input field.
试试这个:
try this:
很难确切地说出你在做什么。
但问题可能是您有两个点击处理程序,一个来自 jQuery,另一个“硬编码”:
onClick="calculImc()"
It's hard to tell exactly what you are doing.
But the problem may be that you have two click handlers, one from jQuery and one 'hard-coded':
onClick="calculImc()"