-3

TypeError: document.getElementById(...) は null です。フォームをクリックして続行すると、このエラーが生成されます。このエラーを修正する方法を教えてください。

続行ボタンをクリックすると、ボタン呼び出し関数送信フォーム

<script type="text/javascript"> 

    function submitForm( )
    {  

        var form = document.paywayForm; 
        var errorString = "";
        var recurringURL = "https://www.payway.com.au/SignUp?ClientNumber=Q12882&&merchant_id=23660327&FirstPaymentDate=&CustomerNumber=&returnURL=&AddressRequired=true&PlanName=";
        // NOTE: THIS USES A TEST MERCHANT!!
        var netURL = "https://www.payway.com.au/MakePayment?BillerCode=128827&merchant_id=23660327&payment_amount=";
            if( document.getElementById("RecurringRadio").checked == true )
        {
            if( document.getElementById("frequency1").value == null || document.getElementById("frequency1").value == "" )
            {
                errorString = errorString + " payment frequency";
            }
            if( document.getElementById("SelectAmount1").value == null || document.getElementById("SelectAmount1").value == "" )
            {
                errorString = errorString + " payment amount";
            }
            if( errorString == "" )
            {
                document.location = recurringURL + document.getElementById("frequency1").value + document.getElementById("SelectAmount1").value +
                                    "&Firstname=" + document.getElementById("OneOffAmount1").value;
            }
        }

        else if( document.getElementById("OneOffRadio").checked == true )
        {
            if( document.getElementById("OneOffAmount1").value == null || document.getElementById("OneOffAmount1").value == "" )
            {
                errorString = errorString + "payment amount &nbsp; &nbsp;";
            }
            if( document.getElementById("Firstname").value == null || document.getElementById("Firstname").value == "" )
            {
                errorString = errorString + "name &nbsp; &nbsp;";
            }
            if( document.getElementById("Surname").value == null || document.getElementById("Surname").value == "" )
            {
                errorString = errorString + "surname &nbsp; &nbsp;";
            }
            if( document.getElementById("Address").value == null || document.getElementById("Address").value == "" )
            {
                errorString = errorString + "address &nbsp; &nbsp;";
            }

            if( document.getElementById("Suburb").value == null || document.getElementById("Suburb").value == "" )
            {
                errorString = errorString + "suburb &nbsp; &nbsp;";
            }
            if( document.getElementById("Postcode").value == null || document.getElementById("Postcode").value == "" )
            {
                errorString = errorString + "postcode &nbsp; &nbsp;";
            }
            if( errorString == "" )
            {    

                 var newredirect = netURL + document.getElementById("OneOffAmount1").value +
"&information_fields=Direct_Funds,Title,Firstname,Surname,Address,Suburb,Postcode,State,Country,Phone,Mobile,Email,Bmail,PAWS&payment_reference=00041234" + 
                                    "&Direct_Funds=" + document.getElementById("Direct_Funds").value + 
                                    "&Title=" + document.getElementById("Title").value + 
                                    "&Firstname=" + document.getElementById("Firstname").value + 
                                    "&Surname=" + document.getElementById("Surname").value + 
                                    "&Address=" + document.getElementById("Address").value +
                                    "&Suburb=" + document.getElementById("Suburb").value +
                                    "&Postcode=" + document.getElementById("Postcode").value +
                                    "&State=" + document.getElementById("State").value +
                                    "&Country=" + document.getElementById("Country").value +
                                    "&Phone=" + document.getElementById("Phone").value +
                                    "&Mobile=" + document.getElementById("Mobile").value +
                                    "&Email=" + document.getElementById("Email").value +
                                    "&Bmail=" + document.getElementById("Bmail").value + 
                                    "&PAWS=" + document.getElementById("PAWS").value;
                              //   alert(newredirect);
                                 document.paywayForm.submit();
                /* document.location.href = newredirect;*/
            }
        }
        else
        {   
            errorString = errorString + "You must select a donation type<br/>";
        }

        if( errorString != "" )
        {   
            //alert(errorString);
            var errorEl = document.getElementById("Error");
            errorEl.innerHTML = "<b>The following fields must be completed: </b><br/> " + errorString + "";
            //alert(errorEl.innerHTML);
        }
    }


</script>
4

5 に答える 5

0

このエラーは、要素が null (存在しない) の場合に発生します。そのため、javascript はその値を取得できません。最初に要素を取得してから、それが null かどうかを確認してから、要素が HTML に表示されているかどうかを知らずに値を直接要求するのではなく、その値を要求できます。

element1 = document.getElementById("frequency1");

if(element1 != null)
{
    //code to set the value variable and test the value not to be empty.
}
于 2013-09-12T05:25:57.390 に答える