简体   繁体   English

何时检查表单输入数据-使用模糊或更改事件?

[英]When to check the form input data - use blur or change event?

Suppose I have a form like this: 假设我有一个这样的表格:

Name:<input type="text" name="xxx"  id="name"/>
Type:<select>
          <option>xx</option>
          <option>xx</option>
      </select>
<input type="submit" disabled="disabled" id="sub" />

Now, I want to check to make sure the user name are unique. 现在,我要检查以确保用户名是唯一的。 And the submit button will be abled only and only if the name is unique. 仅当名称唯一时,才可以使用“提交”按钮。 So I use jQuery AJAX to query the database. 因此,我使用jQuery AJAX查询数据库。

But now I wonder when to check it? 但是现在我想知道何时检查它?

First I use this: 首先我用这个:

$("#name").blur(function () {
    $.get(........);
}

But now, in the modern browser they provide the form data save and auto-filled feature. 但是现在,在现代浏览器中,它们提供了表单数据保存和auto-filled功能。 So when user dbclick the input,the browser will provide a list of data (history data) for user to choose (see the image below). 因此,当用户dbclick输入时,浏览器将提供数据列表(历史数据)供用户选择(请参见下图)。

在此处输入图片说明

When user choose one of them, the blur event can not be triggered. 当用户选择其中之一时,将不会触发blur事件。 I also try to use the change event, but it will cause too many request to the server when user use the keybord to input. 我也尝试使用change事件,但是当用户使用键盘输入时,它将导致对服务器的过多请求。

How to fix this problem? 如何解决这个问题?

UPDATE: None of the answer below is prefect. 更新:下面的答案都不是完美的。 And now I just use a timer in my page to check the "name" changed or not once per 0.5s. 现在,我只需要在页面中使用计时器,就可以每0.5秒检查一次“名称”是否更改。 It seems that it meet my requirements. 看来它符合我的要求。

简单的解决方案:将ajax调用放在密码字段的焦点上。

what you should do is when the user double click on the text box, download the options and store them in an array in your code behind. 您应该做的是当用户双击文本框,下载选项并将它们存储在后面代码中的数组中。 as the user types in the text box, remove the items that are not wanted. 当用户在文本框中键入内容时,请删除不需要的项目。

Also, if you want to fire an event like blur (which is on input element) you probably want to use the change even for drop down list. 另外,如果您要触发诸如blur(在输入元素上)之类的事件,则可能甚至要对下拉列表使用更改。

The approach I would use is to load the collection of Usernames in on page load , and check the user entered data against the collection on the change event . 我将使用的方法是on page load Usernames的集合,并在change event对照该集合检查用户输入的数据。 This way your database will only need to deal with a single request per page load, and while the front end has more work to do, it shouldn't have a negative impact on your user's experience. 这样,您的数据库将只需要处理每个页面加载一个请求,并且尽管前端有更多工作要做,但它不会对用户的体验产生负面影响。

There is a chance for concurrency issues, where two new users load the page at the same time, choose the same username and try to submit, whoever submits last is going to have an active submit button but an invalid username, and you'll need something server side to deal with that. 可能会出现并发问题,其中有两个新用户同时加载页面,选择相同的用户名并尝试提交,最后提交的人将拥有一个活动的提交按钮,但用户名无效,并且您需要服务器端处理的事情。

You could also simply add button for "Check availability" and fire the username uniqueness checking function on the "Check availability" button's click event. 您也可以简单地添加“检查可用性”按钮,并在“检查可用性”按钮的点击事件上触发用户名唯一性检查功能。

Name:<input type="text" name="xxx"  id="name"/> <input type="button" name="checkUsername"  id="checkUsername" value="Check availability"/>
Type:<select>
          <option>xx</option>
          <option>xx</option>
      </select>
<input type="submit" disabled="disabled" id="sub" />

$("#checkUsername").click(function (e) {
    $.get(........);
    e.preventDefault();
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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