簡體   English   中英

如何使用香草javaScript禁用提交按鈕?

[英]How to disable a submit button with vanilla javaScript?

如果存在下拉列表的默認值,我不希望啟用提交按鈕。

在此處輸入圖片說明

這是我的代碼中與我的問題有關的部分:

  function checkSelect(select) {
    if (select.value == '-- select an option --') submit.disabled = true;
    else submit.disabled = false;
  }

  (function() {
    if (checkSelect(starting)) {
      starting.addEventListener('change', function(e) {
        startingEventVal = returnAirPort(e.srcElement.value);
        console.log('startingEventVal', startingEventVal);
      });
    }
    if (checkSelect(destination)) {
      starting.addEventListener('change', function(e) {
        startingEventVal = returnAirPort(e.srcElement.value);
        console.log('startingEventVal', startingEventVal);
      });
    }
  })();

這就是我的全部代碼:

(function() {
  var starting = document.getElementById('starting'),
    destination = document.getElementById('destination'),
    submit = document.getElementById('submit'),
    airportNames = Object.keys(IntentMedia.Airports.airport_distances()),
    startingEventVal,
    destinationEventVal;

  function appendToUL(ul, element) {
    var optionEL = document.createElement('option');
    optionEL.text = element;
    optionEL.value = element;
    return ul.appendChild(optionEL);
  }

  function returnAirPort(airport) {
    return airport;
  }

  function checkSelect(select) {
    if (select.value == '-- select an option --') submit.disabled = true;
    else submit.disabled = false;
  }

  airportNames.forEach(function(element) {
    appendToUL(starting, element);
    appendToUL(destination, element);
  });

  (function() {
    if (checkSelect(starting)) {
      starting.addEventListener('change', function(e) {
        startingEventVal = returnAirPort(e.srcElement.value);
        console.log('startingEventVal', startingEventVal);
      });
    }
    if (checkSelect(destination)) {
      starting.addEventListener('change', function(e) {
        startingEventVal = returnAirPort(e.srcElement.value);
        console.log('startingEventVal', startingEventVal);
      });
    }
  })();

  submit.addEventListener('click', function(e) {
    e.preventDefault();
    var distance = IntentMedia.Distances.distance_between_airports(startingEventVal, destinationEventVal);
    console.log('distance', distance);

    document.getElementById('feedback').innerHTML = distance;
  });
})(); 

任何幫助,將不勝感激!

修復此功能:

//This is wrong because select.value refers to the value property of the select
//and you are comparing that to the selected option's text
function checkSelect(select) {
    if (select.value == '-- select an option --') submit.disabled = true;
    else submit.disabled = false;
  }

更改為此:

//Compare the selected option's text to see if it equals the default option text
function checkSelect(select) {
    if (select.options[select.selectedIndex].text == '-- select an option --') submit.disabled = true;
    else submit.disabled = false;
  }

在click事件中,嘗試檢查if語句中輸入的值,然后執行以下操作:

return;

如果它們是默認值,則退出該函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM