使用一个回调函数,而不是平变化的

问题描述 投票:0回答:1

我有一个的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));
    });
      }
javascript jquery callback onchange
1个回答
0
投票
   $( "#foo" ).on( "click", function() {
  console.log( 22 );
});  
 <div id="foo">asas</div>

更多请参考这里:http://api.jquery.com/on/

© www.soinside.com 2019 - 2024. All rights reserved.