使用回调函数代替onchange

时间:2019-02-04 17:14:51

标签: javascript jquery callback onchange

我有一个表单,其中jQuery进行了一些基本更改,所有这些都正常工作...目前,我必须使用onchange="state()"才能调用脚本。

我对Javascript和JQuery真的很陌生,想知道这是否真的是最好的解决方案,我当时在考虑使用回调函数,而可能使用on()来代替,但似乎无法正常工作,这是最佳解决方案以及如何实现回调函数(即使它不是我只是感兴趣的最佳解决方案)

谢谢

let cWebsite = ["Communicate a message", "Build trust in our brand", "Sell our product", "Get our content online", "Realign our brand", "Engage a new audience"];
let iWebsite = ["make our site more discoverable", "increase engagement", "make it more user friendly", "improve accessiblity", "add more sections", "rebuild the whole thing!"];
let cSeo = ["build on my seo", "make my website faster", "make my website more usable"];
let iSeo = ["improve seo", "reporting my seo", "engage an audience", "All seo services", "compliment other touchpoints"];
let cGraphics = ["Create more graphics", "improve a specific section", "target a new audience", "make it more noticable", "make it stand out"];
let iGraphics = ["test a graphic", "solve a business problem", "track stuff!", "engage an audience", "promote our brand"];

let prices = {
    cWebsite: ["Please Select", "5k - 10k", "10k - 20k", "20k - 30k", "10k - 40k", "60k - 80k", "80k - 100k", "100K+"],
    iWebsite: ["Please Select", "5k - 10k", "10k - 20k", "20k - 30k", "10k - 40k", "60k - 80k", "80k - 100k", "100K+"],
    cSeo: ["Please Select", "£300/mo", "£550/mo", "£1100/mo"],
    iSeo: ["Please Select", "£300/mo", "£550/mo", "£1100/mo"],
    cGraphics: ["Please Select", "£100 - £300", "£300 - £600", "£900 - £1k", "1k - 5k", "5k - 15k", "15k+"],
    iGraphics: ["Please Select", "£100 - £300", "£300 - £600", "£900 - £1k", "1k - 5k", "5k - 15k", "15k+"]
    };

function state() {
    let sta = document.getElementById("state");
    let ser = document.getElementById("service");
    let site = document.getElementById("lives");
    let state = sta.options[sta.selectedIndex].value;
    let service = ser.options[ser.selectedIndex].value;

    if (state === "create") {
        if (service === "website") {
            doJoin(cWebsite);
            priceList(prices.cWebsite);
            site.style.display = "none";
        } else if (service === "seo") {
            doJoin(cSeo);
            priceList(prices.cSeo);
            site.style.display = "block";
        } else if (service === "graphics") {
            doJoin(cGraphics);
            priceList(prices.cGraphics);
            site.style.display = "none";
        }
    } else if (state === "improve") {
        if (service === "website") {
            doJoin(iWebsite);
            priceList(prices.iWebsite);
            site.style.display = "block";
        } else if (service === "seo") {
            doJoin(iSeo);
            priceList(prices.iSeo);
            site.style.display = "block";
        } else if (service === "graphics") {
            doJoin(iGraphics);
            priceList(prices.iGraphics);
            site.style.display = "none";
        }
    }
}



function doJoin(x) {
 $("#choices").empty();
    x.forEach(function (list) {
    let checkbox="<input type='checkbox'><label>"+list+"</label><br>"
    $("#choices").append($(checkbox));
    });
}


function priceList(x) {
 $("#pricelist").empty();
    x.forEach(function (list) {
    let option="<option>"+list+"</option>"
    $("#pricelist").append($(option));
    });
      }

1 个答案:

答案 0 :(得分:0)

   $( "#foo" ).on( "click", function() {
  console.log( 22 );
});  
 <div id="foo">asas</div>

有关更多信息,请参见:http://api.jquery.com/on/