页头部分中的JS并不是正常的

时间:2016-11-09 07:27:24

标签: javascript jquery

我有一个处理半复杂联系表单的脚本。该网站使用名为Duda的网站构建平台构建,我认为可能存在时间问题,因为它们加载了一堆自己的脚本。无论如何我得到一个ReferenceError:价格没有定义,错误。

价格在输入上调用onChange,例如:

<input value="Yes" name="dmform-9" type="radio" id="watch-me-hide" checked="checked" onchange="price()"/>

脚本有时可以工作,其他时候则不行。很沮丧。有谁知道如何在这种情况下确保我的函数被加载?

以下是整个脚本:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
var counter = 0;
$(document).ready(function() {
    $("input[type="radio"]").click(function() {
        alert("here");
        var ids = ["#occupiedBy", "#nameOccupied", "#phoneOccupied", "#mobileOccupied", "#emailOccupied"];

    if($(this).attr("id") == "watch-me-hide" || $(this).attr("id") == "watch-me-show") {

        if ($(this).attr("id") == "watch-me-hide") {

          $("#show-me").hide();

          ids.forEach((id) => $(id).prop("required", false));            

        }

        else if ($(this).attr("id") == "watch-me-show"){

            $("#show-me").show();

            ids.forEach((id) => $(id).prop("required", true)); 
        }
        else {

        }

    } else if($(this).attr("id") == "hideShowInDepth-hide" || $(this).attr("id") == "hideShowInDepth-show") {


            if ($(this).attr("id") == "hideShowInDepth-hide") {
                $("#moreRooms").hide();
                price(null);
                ids.forEach((id) => $(id).prop("required", false));                  
            }
            else if ($(this).attr("id") == "hideShowInDepth-show"){
                $("#moreRooms").show();
                price(null);
                ids.forEach((id) => $(id).prop("required", true));     

            }
        }

        else{

        }
   });

};   


function price() {

    var priceIncrement = 70;
    var minimumPrice = "189.00";
    var basic = document.getElementById("hideShowInDepth-hide");
    var advanced = document.getElementById("hideShowInDepth-show");
    var ids = ["numberAdditionalRooms", "numberLaundries", "bedrooms", "numberLounges", "numberDining", "numberKitchens", "numberBathrooms", "numberHallways", "numberGarages"];

    if ((basic.checked == true) && (advanced.checked == false)) {

        /* Get number of rooms for basic inspection */

        var e = document.getElementById("bedrooms");
        var numberOfBedrooms = e.options[e.selectedIndex].value;

        if (numberOfBedrooms == 0){
           document.getElementById("totalPrice").textContent = "0.00";

        }

        else if ((numberOfBedrooms <= 3) && (numberOfBedrooms >= 1)){
           document.getElementById("totalPrice").textContent = minimumPrice;

        }

        else if ((numberOfBedrooms <= 6) && (numberOfBedrooms >= 3)){
           document.getElementById("totalPrice").textContent = "259.00";

        }

        else if (numberOfBedrooms > 6){
           document.getElementById("totalPrice").textContent = "329.00";

        }

        else {
            alert("Script error: Undefined number of bedrooms")
        }


        return false;
    }

    else if ((basic.checked == false) && (advanced.checked == true)) { 
        /* Get number of rooms for advanced inspection */

        ids.forEach(logArrayElements);

        if (counter == 0){
            document.getElementById("totalPrice").textContent = "0.00";
        }
        else if (counter == 1){
            document.getElementById("totalPrice").textContent = minimumPrice.toFixed(2);
;
        }
        else {
            var money =  ((counter * priceIncrement) + +minimumPrice) - +priceIncrement;
            document.getElementById("totalPrice").textContent = money.toFixed(2);
        }
            counter=0;

        return false;
    }

    else if ((basic.checked == false) && (advanced.checked == false)) {
        alert("Script error: Neither checkbox is checked");
        return false;
    }

    else if ((basic.checked == true) && (advanced.checked == true)) {
        alert("Script error: Both checkboxes are checked");
        return false;
    }
    else{
        alert("Script error: Unknown error");
        return false;
    }
}


function logArrayElements(element, index, array) {
   // alert("a[" + index + "] = " + element + " - " + value);
    var e = document.getElementById(element);
    var strUser = e.options[e.selectedIndex].value;
    counter = +counter + +strUser;
}

</script>

“看我隐藏”和“看我播放”是单选按钮,当它们被更改时,它们会隐藏或显示联系表单的另一部分以填写,并输入所需的输入。

hideShowInDepth-hide和hideShowInDepth-show是相同的,但是对于另一个div部分。

价格函数更新跨度中的文本以反映输入值更改时的新价格。

在下拉列表和单选按钮的更改时调用

price()。

任何帮助表示感谢。

0 个答案:

没有答案