简体   繁体   English

jQuery在keyup上获取带有类的输入的ID无效

[英]jQuery getting ID of input with class on keyup not working

I have a bit of problem with my code here. 我的代码在这里有点问题。 What I am trying to accomplish is to get the ID of an input field on key up. 我要完成的工作是在键上获取输入字段的ID。 All the input fields of interest have a class "count". 所有感兴趣的输入字段都有一个“计数”类。 All of them also have the same ID but it increments, say field1, field2, field3. 它们全部都具有相同的ID,但是它递增,例如field1,field2,field3。 I also have some "answer" inputs. 我也有一些“答案”输入。

The mechanism I am trying to accomplish is to first get the ID from the input where I am typing (keyup). 我试图实现的机制是首先从我正在键入的内容(键)中获取ID。 Then I proceed to pass in that value (var valueField) to the function updateFields(idNumber). 然后,我继续将该值(var valueField)传递给函数updateFields(idNumber)。 After that, I just simply add 100 to the value of the field#x and finally I append the final value to its matching input with the id of answerx. 之后,我只需将field#x的值加100,最后将最终值附加到其具有answerx的ID的匹配输入中。

My code is not working. 我的代码无法正常工作。 Does anybody know why? 有人知道为什么吗? Is this approach correct? 这种方法正确吗? Or would you advise me to change something? 还是您建议我更改某些内容?

Here's my code: 这是我的代码:

HTML: HTML:

<input type="text" placeholder="" class="count" name="input1" id="1"/>
<input type="text" placeholder="" class="count" name="input2" id="2"/>
<input type="text" placeholder="" class="count" name="input3" id="3"/>

<input type="text" placeholder="" class="answer" name="ans1" id="answer1"/>
<input type="text" placeholder="" class="answer" name="ans2" id="answer2"/>
<input type="text" placeholder="" class="answer" name="ans3" id="answer3"/>

JavaScript: JavaScript的:

    $(document).on('keyup','.count',function(){
        var valueField = $(this).attr('id').val();
        updateFields(valueField);
    });

    function updateFields(idNumber){

        var rawVal = $('#field' + idNumber).val();
        var final = rawVal + 100;

        $('#answer' + idNumber).val(final)

    }

Thanks in advance! 提前致谢!

When you get the attr('id') you don't need to call .val() . 当您获得attr('id')您无需调用.val() Also your code to get the field assumes a id that starts with 'field' and finally if you are going to take text input and treat is a number you should call parseInt() (with a radix of 10 for decimal), this will work: 同样,您获取字段的代码还假设一个以'field'开头的id,最后,如果您要输入文本并且将数字视为数字,则应调用parseInt() (十进制的基数为10) :

 $(document).on('keyup', '.count', function() { var valueField = $(this).attr('id'); updateFields(valueField); }); function updateFields(idNumber) { var rawVal = parseInt($('#' + idNumber).val(), 10); var final = rawVal + 100; $('#answer' + idNumber).val(final) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" placeholder="" class="count" name="input1" id="1" /> <input type="text" placeholder="" class="count" name="input2" id="2" /> <input type="text" placeholder="" class="count" name="input3" id="3" /> <input type="text" placeholder="" class="answer" name="ans1" id="answer1" /> <input type="text" placeholder="" class="answer" name="ans2" id="answer2" /> <input type="text" placeholder="" class="answer" name="ans3" id="answer3" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM