我无法让 .val() 清除用户输入,我不知道为什么它不起作用



因此,每当用户按enter键提交输入时,我都会尝试清除文本输入。我试过$("#textbox").val(''),但它不起作用,我不知道我做错了什么。我也试过.html('').text('')。我不知道为什么它不起作用。

这是我的编码。

// JavaScript Document
$(document).ready(function() {
  $('a').click(function() {
    $('#start').css('background-color', '#09C');
  });
});
//Item variable//
$(document).ready(function() {
  $("form").submit(function() {
    var input = $("#command").val();
    var check = false;
    if (input == "help") {
      $("#Help").clone().insertBefore("#placeholder").fadeIn(1000);
      check();
    }
    if (input == "Lombardy East") {
      $("<p class='text'> Great Choice! Close enough to Alexandra to have access to their commuters but far enough not to step on anyone's toes! Now it's time to choose your route. Shall we?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      $("<p id='green' class='text'>Let's do this?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      check();
      check();
    }
    if (input == "Lyndhurst") {
      $("<p class='text'> It's good enough choice. As long as you don't clash with other drivers on the main road, you should be fine. Now it's time to choose your route. Shall we?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      $("<p id='green' class='text'>Okay?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      check();
      check();
    }
    if (input == "Orange Grove") {
      $("<p class='text'>Hmmm ... That's a bit close to Louis Botha main road. You might have a few aggressive run ins. Good Luck with that.Now it's time to choose your route. Shall we?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      $("<p id='green' class='text'>Sure?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      check();
      check();
    }
    if (input == "No") {
      $("<p class='text'> Voetsak! Don't be a wuss!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      check();
    }
    if (input == "Yes") {
      var url = "thirdpage.html";
      $(location).attr('href', url);
      check();;
    }
    if (input == "Let's do this") {
      var url = "frthpage.html";
      $(location).attr('href', url);
      check();
    }
    if (input == "Okay") {
      var url = "ffthpage.html";
      $(location).attr('href', url);
      check();
    }
    if (input == "Sure") {
      var url = "sxthpage.html";
      $(location).attr('href', url);
      check();
    } else if (input != "Lombardy East") {
      $("<p class='text'> I do not understand your answer</p>").hide().insertBefore("#placeholder").fadeIn(1000);
      check();
    }
    $('#command').val("");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="placeholder"></div>
<form onsubmit="return false" autocomplete="off">
  <input type="text" size="50" autofocus="autofocus" id="command" />
</form>

问题不在于您试图清除输入的方式。

$('#command').val("");

这将起作用,但是您在页面上有一个javascript错误。

问题是您的检查变量,您将变量检查初始化为基元类型。

var check = false; 

这是一个布尔值,而不是函数。因此,当您试图调用变量check时,由于它不是一个函数,您将得到一个错误。

我不确定您试图通过check函数调用实现什么,但您有两种选择。

1.)移除所有check();由于check是基元类型(boolean),因此不能将其作为函数调用。

2.)你可以把检查作为一个函数。。。

var check = function() {
    // Do Logic here
} 

相关内容

最新更新