无法读取null验证输入

时间:2016-09-22 18:52:56

标签: javascript validation javascript-events addeventlistener

我发现错误:

  

无法读取null

的属性'addEventListener'

我有两个输入事件和另一个div。 Nly输入事件显示此错误,如何防止它?当我将这个脚本添加到相应的页面时它触发了 - 但是在main.js文件中我遇到了这个错误。

<form>
   <div class="form-row">
       <label>Vælg et beløb</label>
          <div class="doner-prices">50</div>
          <div class="doner-prices">100</div>
          <div class="doner-prices">200</div>
          <div class="doner-prices">500kr</div>
   </div>
   <fieldset class="form-row">
        <div class="form-col doner-price">
           <label for="donate-price">
             eller angiv dit eget
            <input type="text" id="donate-price" name="name" pattern="\d+" value="" required>

            </label>
        </div>
     </fieldset>
 </form>   

document.addEventListener("DOMContentLoaded", function () {
    var amount_list = document.querySelectorAll('.form-row .doner-      prices'); //node-list
    var amount_array = [].slice.call(document.querySelectorAll(".form-row   .doner-prices")); //node-list to array
    var donerForm = document.getElementById('doner-hidden');
    var inputDonateField = document.getElementById('donate-price');
    var inputNumber = /^[0-9]+$/;
    var onClickFormVisible = function () {
        donerForm.style.display = "block";
    };
    var onInputTypeNumber = function () {
        if (inputNumber.test(inputDonateField.value)) {
            donerForm.style.display = "block";
        } else {
            return false;
        }
    };
    //onclick event for each amount images
    var amoutn_array = amount_array.map(function (e) {
        return e.addEventListener('click', onClickFormVisible, false);
    });
    //input event only if value === number
    inputDonateField.addEventListener("keyup", onInputTypeNumber, false);
});

0 个答案:

没有答案