结合使用jQuery脚本使其更小

时间:2017-03-14 12:44:06

标签: javascript jquery

我编写了一个jQuery脚本,它执行相同的任务,但在不同的事件上说

  1. 页面加载
  2. 更改下拉选项
  3. 点击交换按钮
  4. 关于文本字段中的键盘
  5. 但我必须分别为所有这些脚本编写脚本。我是jQuery的新手。我希望将脚本组合起来,但要使其更小。有可能吗?

    脚本

    SELECT REGEXP_REPLACE(
             rate_information,
             '^(\d+\.\d+ - \d+\.\d+)(.*)$',
             TO_CHAR( new_value, 'FM9999999990.0' ) || '\2'
           )
    FROM   your_table;
    

3 个答案:

答案 0 :(得分:2)

这更像是一般性问题,但现在是。您可以重用您的代码!只需将代码放入类似的函数中即可。

function hello(){ console.log('hi!')}

然后你可以将它传递给另一个函数

anotherFunction(hello)

或者简单地称之为

hello()

在你的例子中,你可以这样做

function someFunction() {
    $.getJSON(
        'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
        function (data) {
            if (typeof fx !== "undefined" && fx.rates) {
                fx.rates = data.rates;
                fx.base = data.base;
                var amount = $("#amt").val();
                var from = $("#from").val();
                var to = $("#to").val();
                $("#res").val(fx(amount).from(from).to(to));
                $("#result").show();
            }
        }
    );
}
function passItToSwap(e) {
    e.preventDefault();
    var fromVal = $("#from option:selected").val();
    var fromText = $("#from option:selected").text();
    var toVal = $("#to option:selected").val();
    var toText = $("#to option:selected").text();

    $("#from option:selected").val(toVal);
    $("#from option:selected").text(toText);
    $("#to option:selected").val(fromVal);
    $("#to option:selected").text(fromText);

    someFunction();
}

$(document).ready(someFunction);
$(document).keyup('#amt', someFunction);
$("#swap").click(passItToSwap);

答案 1 :(得分:1)

您可以为每个处理程序重用相同的函数,因为您似乎在每个处理程序中都有相同的代码:

var fx = {
        'base' : null,
        'rates' : true
    },
    getRates = function() {
        $.getJSON( 'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5', function(data) {
            var amount,
                from,
                to;
            if (typeof fx !== "undefined" && fx.rates) {
                fx.rates = data.rates;
                fx.base = data.base;
                amount = $("#amt").val();
                from = $("#from").val();
                to = $("#to").val();
                $("#res").val( fx(amount).from(from).to(to));
                $("#result").show();
            }
        });
    };
// Fire this function when page loads
$(document).ready(getRates);
// Fire this function when value is entered in the field
$(document).keyup('#amt', getRates);
// Fire this function on swap button click
$("#swap").click(function(e) {
    e.preventDefault();
    var fromVal = $("#from option:selected").val(),
        fromText = $("#from option:selected").text(),
        toVal = $("#to option:selected").val(),
        toText = $("#to option:selected").text();

    $("#from option:selected").val(toVal);
    $("#from option:selected").text(toText);
    $("#to option:selected").val(fromVal);
    $("#to option:selected").text(fromText);
    getRates();
});
// Fire this function on change of "FROM" and of "TO" dropdown selection
$("#from, #to").change(getRates);

答案 2 :(得分:0)

您可以在功能中划分代码并创建单独的文件(filename.js)以将其包含在HTML中。