简体   繁体   English

如何在 JS 中组合这些功能

[英]How Can I Combine This Functions in JS

It's increasing a form value of total votes on star rating.它增加了星级评分的总票数。 if click 1 star, increases votes +1, if click 5 stars, increases 5. How can i combine them?如果点击 1 星,则增加投票 +1,如果点击 5 星,则增加 5。我该如何组合它们?

    function incrementValue1() {
    var value = parseInt(document.getElementById('ratecount').value, 10);
value = isNaN(value) ? 0 : value;
value++;
    document.getElementById('ratecount').value = value;
}
function incrementValue2() {
    var value = parseInt(document.getElementById('ratecount').value, 10);
value = isNaN(value) ? 0 : value;
value+=2;
    document.getElementById('ratecount').value = value;
}
function incrementValue3() {
    var value = parseInt(document.getElementById('ratecount').value, 10);
value = isNaN(value) ? 0 : value;
value+=3;
    document.getElementById('ratecount').value = value;
}
function incrementValue4() {
    var value = parseInt(document.getElementById('ratecount').value, 10);
value = isNaN(value) ? 0 : value;
value+=4;
    document.getElementById('ratecount').value = value;
}
function incrementValue5() {
    var value = parseInt(document.getElementById('ratecount').value, 10);
value = isNaN(value) ? 0 : value;
value+=5;
    document.getElementById('ratecount').value = value;
}

Pass the increment value as a parameter to one function like:将增量值作为参数传递给一个 function,如:

function incrementValue(amt) {
    var value = parseInt(document.getElementById('ratecount').value, 10);
    value = isNaN(value) ? 0 : value;
    value+=amt;
    document.getElementById('ratecount').value = value;
}

Just add a parameter to the function that indicates how much to increment the value.只需在 function 中添加一个参数,指示该值增加多少。

function incrementValue(increment) {
    var value = parseInt(document.getElementById('ratecount').value, 10);
    value = isNaN(value) ? 0 : value;
    value += increment;
    document.getElementById('ratecount').value = value;
}
incrementValue(1);//increment by 1
incrementValue(5);//increment by 5

All you need to do is take advantage of parameters in functions , which are data that is passed into the function when it is called.您需要做的就是利用函数中的参数,这些参数是在调用 function 时传递给它的数据。 That data (parameter) is available as a local variable within the function.该数据(参数)可用作 function 中的局部变量。

In this case, you would pass the amount that you need to increment by because everything else in all those functions is the same:在这种情况下,您将传递需要增加的数量,因为所有这些函数中的其他所有内容都是相同的:

 // You should get your element references just once, not // everytime the function is called: let rateCount = document.getElementById('ratecount'); // This function is declared in a way that makes it // expect some data will be passed to it when it is // invoked and that data will be accessible within // the function using the parameter name defined // by the function declaration (amount in this case) function incrementValue(amount) { var value = parseInt(rateCount.value, 10); value = isNaN(value)? 0: value; value += amount; // Use the passed in amount rateCount.value = value; } incrementValue(16);
 <input id="ratecount">

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

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