简体   繁体   中英

Form Input Not Accepting Spaces

I've got a form using JavaScript that sends a user to a different page depending on their input using "if/else" statements. However, the input field will not accept spaces. It will accept every character except spaces. I can't seem to figure out what is causing this.

EDIT: I have added the HTML for the entire form, including the submit button.

EDIT 2: I have also updated my JS to reflect the 'switch' statements, rather than else if, as recommended by Mister Jojo.

EDIT 3: Just to clarify, if I past spaces in, it will work. But if I just type them it will not show a space.

Below is the HTML, CSS, and JS that goes along with the input form.

better to code https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

 function checkAnswer(){
  let response = document.getElementById('state').value;

  switch (response) {
    case 'Kentucky':
    case 'kentucky':
    case 'KY':
    case 'ky':
    case 'New York':
    case 'new york':
    case 'NY':
    case 'ny':
    case 'Washington':
    case 'Washington St':
    case 'Washington ST':
    case 'Washington st':
    case 'washington':
    case 'washington state':
    case 'Washington State':
    case 'Washington state':
    case 'washington State':
    case 'wa':
    case 'WA':
    location = 'xyz.com'; // 'right.html';
      break;

    default:location = 'https://google.com'; // 'wrong.html';
      break;
  }

  return false;
}

I've solved it! It was a script that I had linked to the page that was affecting spaces, I'm not completely sure what the contents of the script were that affected spaces, but nonetheless the issue is solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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