简体   繁体   English

HTML表单输入值未在javascript函数中更新

[英]HTML Form input value not updating in javascript function

I have a simple HTML form which has an event listener binded to it and when you click on the button inside the form that has a class of 'booking__form__counter--increase' this should increase the input field value by 1. It calls a javascript function named 'increaseCounter()' I declare a variable that points to this value but when i try to use the variable to increment it, it doesn't work. 我有一个简单的HTML表单,其中绑定了一个事件侦听器,当您单击表单中具有“ booking__form__counter--increase”类的表单上的按钮时,这会将输入字段的值增加1。它调用了javascript函数名为“ increaseCounter()”的我声明了一个指向该值的变量,但是当我尝试使用该变量来递增它时,它将无法正常工作。 If i use the methods in the variable directly it works? 如果我直接使用变量中的方法,它会起作用吗? I am missing something simple here but i cannot work out what. 我在这里错过了一些简单的事情,但我无法解决。

 let bookingForm = document.querySelector('.booking__form'); bookingForm.addEventListener('click', function (e) { let target = e.target; let inputCounterValue = target.parentElement.firstElementChild.value; let inputMaxCounterValue = target.parentElement.firstElementChild.dataset.maxCount; let showCounterValue = target.parentElement.firstElementChild.nextElementSibling.textContent; if (target.classList.contains('booking__form__counter--increase')) { increaseCounter(); } function increaseCounter() { if (inputCounterValue === inputMaxCounterValue) { return; } else { //does not update inputCounterValue++; showCounterValue = inputCounterValue; //this does update target.parentElement.firstElementChild.value++; target.parentElement.firstElementChild.nextElementSibling.textContent = target.parentElement.firstElementChild.value; } } }); 
 <form class="booking__form"> <div class="container"> <div class="booking__form__group"> <div class="booking__form__section booking__form__section--arrival"> <div class="booking__form__control"> <label for="arrival">Arrival Date</label> <div class="booking__form__counter"> <span class="booking__form__counter--value">0</span> <div class="booking__form__counter--button booking__form__counter--increase"> <svg class="fal fa-chevron-up"></svg> </div> <div class="booking__form__counter--button booking__form__counter--decrease"> <svg class="fal fa-chevron-down"></svg> </div> </div> </div> </div> <div class="booking__form__section booking__form__section--duration"> <div class="booking__form__control"> <label for="arrival">Nights</label> <div class="booking__form__counter"> <input type="hidden" name="duration" value="1" data-max-count="21"> <span class="booking__form__counter--value">1</span> <div class="booking__form__counter--button booking__form__counter--increase"> <svg class="fal fa-chevron-up"></svg> </div> <div class="booking__form__counter--button booking__form__counter--decrease"> <svg class="fal fa-chevron-down"></svg> </div> </div> </div> </div> <div class="booking__form__section booking__form__section--adults"> <div class="booking__form__control" id="booking--adults"> <label for="arrival">Adults</label> <div class="booking__form__counter"> <input type="hidden" name="adults" value="1" data-max-count="8"> <span class="booking__form__counter--value">1</span> <div class="booking__form__counter--button booking__form__counter--increase"> <svg class="fal fa-chevron-up"></svg> </div> <div class="booking__form__counter--button booking__form__counter--decrease"> <svg class="fal fa-chevron-down"></svg> </div> </div> </div> </div> <div class="booking__form__section booking__form__section--children"> <div class="booking__form__control" id="booking--children"> <label for="arrival">Children</label> <div class="booking__form__counter"> <input type="hidden" name="children" value="0" data-max-count="5"> <span class="booking__form__counter--value">0</span> <div class="booking__form__counter--button booking__form__counter--increase"> <svg class="fal fa-chevron-up"></svg> </div> <div class="booking__form__counter--button booking__form__counter--decrease"> <svg class="fal fa-chevron-down"></svg> </div> </div> </div> </div> </div> </div> </form> 

UPDATED Javascript 更新的Javascript

I have had a play around and added my updated javascript below which now seems to be working ok. 我玩了一会儿,并在下面添加了更新的JavaScript,现在看来工作正常。 I removed the data attributes 'data-max-count' and just added in the 'max' attribute and changed the variable decelerations around. 我删除了数据属性“ data-max-count”,仅添加了“ max”属性,并更改了变量减速度。

let bookingForm = document.querySelector('.booking__form');
bookingForm.addEventListener('click', function (e) {
    let target = e.target;
    let input = target.parentElement.firstElementChild;
    let displayValue = target.parentElement.firstElementChild.nextElementSibling;

    if (target.classList.contains('booking__form__counter--increase')) {
        increaseCounter();
    } else if (target.classList.contains('booking__form__counter--decrease')) {
        decreaseCounter();
    }

    function increaseCounter() {
        if (input.value === input.max) {
            return;
        } else {
            input.value++;
            displayValue.textContent = input.value;
        }
    }
}); 

I re-wrote your js and it now works. 我重新编写了您的js,现在可以使用了。 You had some issues with your selectors and the way you updated the values. 您的选择器和更新值的方式存在一些问题。 I associated the max-count with the hidden input you have there and read the data-max-count attribute value. 我将最大计数与您在那里的隐藏输入相关联,并读取了data-max-count属性值。 If this is not present then the auto-increment doesn't work because I set the initial value of inputMaxCounterValue equal to 0. Keep in mind that I only update what the user sees and not the input value. 如果不存在此值,则自动增量将不起作用,因为我将inputMaxCounterValue的初始值设置为0。请记住,我只更新用户看到的内容,而不更新用户输入的内容。

let bookingForm = document.querySelector('.booking__form');
bookingForm.addEventListener('click', function (e) {
let target = e.target;
let parentElem = target.parentElement;
let inputCounterValue = 0;
let valueContainer = parentElem.querySelector('.booking__form__counter--value');
if (typeof valueContainer.textContent!=="undefined") {
  inputCounterValue = parseInt(valueContainer.textContent,10);
}

if (target.classList.contains('booking__form__counter--increase')) {
    increaseCounter(valueContainer);
}

function increaseCounter(element) {
  let inputMaxCounterValue = 0;
  let parentElem = target.parentElement;

  if (typeof parentElem.querySelector('input')!=="undefined" && parentElem.querySelector('input')!==null) {
    inputMaxCounterValue = parentElem.querySelector('input').getAttribute("data-max-count");
  }
if (inputCounterValue === inputMaxCounterValue) {
    return;
} else {
    //does not update
    inputCounterValue++;
    showCounterValue = inputCounterValue;
    //this does update
    element.textContent = inputCounterValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM