css 表单输入不接受空格

6ioyuze2  于 2023-06-07  发布在  其他
关注(0)|答案(3)|浏览(430)

我有一个使用JavaScript的表单,它根据用户使用“if/else”语句的输入将用户发送到不同的页面。但是,输入字段不接受空格。它将接受除空格以外的所有字符。我不知道是什么原因造成的。
编辑:我已经为整个表单添加了HTML,包括提交按钮。
编辑2:我还更新了我的JS,以反映'switch'语句,而不是像Mister Jojo推荐的那样。
编辑3:只是为了澄清,如果我过去的空间,它会工作。但是如果我只是输入它们,它就不会显示空格。
下面是与输入表单沿着使用的HTML、CSS和JS。

<!-- This is the HTML for the entire form -->

    <<form onSubmit="return checkAnswer();">
                                              <input class="nice-select wide" placeholder="Enter Your State" id="state" type="text" maxlength="55" class="box" autofocus />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--<p>You have to pay: <span>$0</span></p>-->
                            <div class="submit_btn">
                                <button class="boxed-btn3" value="SUBMIT" type="submit">Continue</button>
                            </div>
                          </form>
<!-- This is the script that is in the head of the document controlling the redirect. -->

    <script>
    function checkAnswer(){
      var response = document.getElementById('state').value;

      if (response == "Kentucky")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "kentucky")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "KY")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "ky")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "New York")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "new york")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "NY")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "ny")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington St")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington ST")
        location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington st")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "washington")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "washington state")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington State")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "Washington state")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "washington State")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "wa")
      location = 'https://linxdalton.com'; // 'right.html';

      else if (response == "WA")
      location = 'https://linxdalton.com'; // 'right.html';

      else
      location = 'https://google.com'; // 'wrong.html';
      return false;
    }
    </script>

下面是与input元素关联的所有CSS。

.nice-select::after {
  content: "\f0d7";
  display: block;
  pointer-events: none;
  position: absolute;
  right: 30px;
  top: 50%;
  transition: all 0.15s ease-in-out;
  width: 5px;
  font-family: 'fontawesome';
  color: #ddd;
  transform: translateY(-50%);
  font-size: 16px;
  right: 25px;
  color: #AAB1B7;
  height: auto;
  margin-top: 0;
}

-webkit-writing-mode: horizontal-tb !important;
    text-rendering: auto;
    color: -internal-light-dark-color(black, white);
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    -webkit-appearance: textfield;
    background-color: -internal-light-dark-color(white, black);
    -webkit-rtl-ordering: logical;
    cursor: text;
    margin: 0em;
    font: 400 11px system-ui;
    padding: 1px;
    border-width: 2px;
    border-style: inset;
    border-color: initial;
    border-image: initial;
}

-webkit-tap-highlight-color: blue;
    background-color: #fff;
    border-radius: 5px;
    border: solid 1px #e8e8e8;
    box-sizing: border-box;
    clear: both;
    display: block;
    float: left;
    font-family: inherit;
    font-size: 14px;
    font-weight: normal;
    height: 42px;
    line-height: 40px;
    outline: none;
    padding-left: 18px;
    padding-right: 30px;
    position: relative;
    text-align: left !important;
    -webkit-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
    white-space: nowrap;
    width: auto;
}

    width: 100%;
    height: 50px;
    line-height: 50px;
    border-radius: 5px;
    color: #7A838B;
    margin-bottom: 15px;

这就是与输入元素连接的所有代码。让我知道如果你能找到任何理由,它不会接受空间!
谢谢!

azpvetkf

azpvetkf1#

更好的代码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;
}
a11xaf1n

a11xaf1n2#

我解决了!这是一个脚本,我已经链接到页面,是影响空间,我不完全确定是什么内容的脚本是影响空间,但尽管如此,这个问题是解决了。

w8ntj3qf

w8ntj3qf3#

我在Angular项目中遇到了同样的问题,由于以下脚本

fromEvent(window, 'keydown')
  .subscribe((event: any) => {
  if ([32, 37, 38, 39, 40].indexOf(event.keyCode) > -1) {
    event.preventDefault();
  }
});

因为空格键码是32,所以可以防止在所有输入中键入空格。删除此脚本或将window替换为nativeElement,将脚本应用于特定组件。

相关问题