How do I add a simple jQuery script to WordPress?

后端 未结 18 1868
心在旅途
心在旅途 2020-11-22 16:53

I read the Codex and a few blog posts about using jQuery in WordPress, and its very frustrating. I\'ve got as far as loading jQuery in functions.php file, but a

18条回答
  •  忘了有多久
    2020-11-22 17:36

    There are many tutorials and answers here how to add your script to be included in the page. But what I couldn't find is how to structure that code so it will work properly. This is due the $ being not used in this form of JQuery.

    So here is my code and you can use that as a template.

    jQuery(document).ready(function( $ ){
      $("#btnCalculate").click(function () {
            var val1 = $(".visits").val();
            var val2 = $(".collection").val();
            var val3 = $(".percent").val();
            var val4 = $(".expired").val();
            var val5 = $(".payer").val();
            var val6 = $(".deductible").val(); 
            var result = val1 * (val3 / 100) * 10 * 0.25;
            var result2 = val1 * val2 * (val4 / 100) * 0.2;
            var result3 = val1 * val2 * (val5 / 100) * 0.2;
            var result4 = val1 * val2 * (val6 / 100) * 0.1;
            var val7 = $(".pverify").val();
            var result5 = result + result2 + result3 + result4 - val7;
            var result6 = result5 * 12;
            $("#result").val("$" + result);
            $("#result2").val("$" + result2);
            $("#result3").val("$" + result3);
            $("#result4").val("$" + result4);
            $("#result5").val("$" + result5);
            $("#result6").val("$" + result6);
      });
    });
    

提交回复
热议问题