输入值上的 jQuery 切换类

发布于 2025-01-07 11:01:15 字数 1389 浏览 4 评论 0原文

我想根据文本输入的输入添加。 我的问题是输入呈现所有类。 这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩动你心 2025-01-14 11:01:15

您在

此外,您还可以大大简化您的代码,特别是如果类名始终是输入字段中的最后一个字母。

$('#calcul').on'click', function(){
    var class = $('#interpretation').val().slice(-1).toLowerCase();
    $('#interpretation').toggleClass(class);
});

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.

$('#calcul').on'click', function(){
    var class = $('#interpretation').val().slice(-1).toLowerCase();
    $('#interpretation').toggleClass(class);
});
挽你眉间 2025-01-14 11:01:15

试试这个:

$('#calcul').on("click", function() {

    var target = $("#interpretation");
    var val =  target.val();
    var className = "";
    if(val == "itemA")                     
    {
        className = "a";
    }
    else if(val == "itemB")                     
    {
        className = "b";
    }
    else if(val == "itemC")                     
    {
        className = "c";
    }
    //............
    target.attr("class", className );
});

try this:

$('#calcul').on("click", function() {

    var target = $("#interpretation");
    var val =  target.val();
    var className = "";
    if(val == "itemA")                     
    {
        className = "a";
    }
    else if(val == "itemB")                     
    {
        className = "b";
    }
    else if(val == "itemC")                     
    {
        className = "c";
    }
    //............
    target.attr("class", className );
});
花辞树 2025-01-14 11:01:15

很难确切地说出你在做什么。

但问题可能是您有两个点击处理程序,一个来自 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()"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文