【问题标题】:Input field must not be empty输入字段不能为空
【发布时间】:2015-07-15 10:33:08
【问题描述】:

我有一个输入字段,除非您键入/选择某些内容,否则该字段不能让您搜索。我该怎么做呢?

<input type="text" class="form-control pg-csr-customer-search CSRinputBut" aria-label="" id="CustomerSearchText" name="CustomerSearchText" value="" placeholder="Search Customer" required>

当用户点击或敲击回车时,我需要显示此警告。

displayCustomerSearchMessage('<i class=\"fa fa-exclamation-circle pull-left\"></i> Sorry, missing selected Customer Id. Please make a selection from the results listing.', 'warning');

【问题讨论】:

  • 向我们展示您的“搜索”事件处理程序...我认为您需要执行event.preventDefault 并在输入为空的情况下显示消息

标签: javascript twitter-bootstrap


【解决方案1】:
<input type="text" name="CustomerSearchText" id="CustomerSearchText" onblur="checkTextField(this);" />


//modify following code as your need
function checkTextField(field) {
    if (field.value == '') {
        alert("empty!!!");
    }
}

更新

<form onsubmit="return checkTextField(this)">


function checkTextField(form) {
  var text = form.CustomerSearchText.value;

    if (text == '') {
            return false
        }


  return true;
}

【讨论】:

  • 这让我可以点击并输入,但是当我点击外部时,我会收到警报。我不希望用户能够只按 Enter 键并获得结果,我需要显示一条消息,例如:字段不能为空或类似的内容。
  • 你可以使用表单提交的逻辑来阻止表单被发布
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
  • 2019-10-27
  • 2022-08-20
  • 2015-06-27
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多