简体   繁体   English

如何为多个文本框重复JQuery函数?

[英]How to repeat JQuery function for multiple text boxes?

I have a simple function I need to repeat 3 times for 3 seperate controls. 我有一个简单的功能,需要对3个单独的控件重复3次。 It seems silly to replicate the same function 3 times however I don't myself know how to do this. 复制相同的函数3次似乎很愚蠢,但是我自己不知道该怎么做。

<script type="text/javascript">
$(document).ready(function () {
//        $("#LeaveHoursTextBox").blur(function () {
      $("input[type=text][id*=LeaveHoursTextBox]").blur(function () {
        alert("On Blur");
        var num1 = parseInt(document.getElementById("LeaveHoursTextBox").value);
        var num2 = parseInt(document.getElementById("LeaveHours2TextBox").value);
        var num3 = parseInt(document.getElementById("LeaveHours3TextBox").value);
        var TotalTime = num1 + num2 + num3;
        document.getElementById("HoursTextBox").value = TotalTime;
    })
});
</script>

The above code works when leaving the first text box. 上面的代码在离开第一个文本框时有效。 Instead of copying and pasting for the other 2 text boxes how would I add them into this same code element ? 与其复制和粘贴其他2个文本框,不如将它们添加到同一代码元素中?

$("#LeaveHoursTextBox,#otherTextBox,#thirdTextBox")...

Could you not give them a common class name? 您能给他们一个普通的班级名字吗? This seems to be the cleaner solution. 这似乎是更清洁的解决方案。

$('.textBoxes') $('。textBoxes')

returns -> [textbox1, textbox2, textbox4] 返回-> [textbox1,textbox2,textbox4]

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

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