简体   繁体   English

jQuery初学者-按时间启动的功能

[英]jquery beginner - function to initiate by time

I am a rookie in JS, have a problem understanding JQUERY semantics. 我是JS的新手,在理解JQUERY语义时遇到问题。

I have a function written for checking the content of a cell. 我编写了一个用于检查单元格内容的函数。

Problem: the function just starts when the cell loses focus, if I click Submit, the error shows first, then it will run the function. 问题:当单元格失去焦点时,该函数才启动,如果单击“提交”,则首先显示错误,然后它将运行该函数。

I want the function to run even when I am inside the cell. 我希望函数即使在单元格中也能运行。 How to do it? 怎么做?

Initiated by this: 以此发起:

$(".user_id").blur(function(){ validateUserId($('.user_id')); });

The function: 功能:

function validateUserId(reference) {
    if ( 5 == $(reference).val().length ) {
        $.get('index.php?user=' + $(reference).val(), function(data) {
            if ( "error" == data ) {
                $(reference).parent().parent().addClass('error');
                alert('error');
            } else {
                $(reference).parent().parent().removeClass('error');
                $(reference).addClass('valid');
                $(reference).parent().parent().addClass('success');
            }
        });
    } else {
        alert('short');
        $(reference).parent().parent().addClass('error');
    }
}
$(".user_id").on('keyup', function(){
   validateUserId($(this)); 
});

i would use the keyup event. 我会使用keyup事件。 So everytime somebody types a key in your input cell, the function will be executed. 因此,每当有人在您的输入单元格中键入一个键时,该函数就会被执行。

 $(".user_id").on("keyup", function(){ validateUserId($(this)); });

I changed the $(".user_id"), you take the value from ,to $(this). 我更改了$(“。user_id”),您将值从更改为$(this)。 Since you want the value of the field you did the keyup event on. 由于您需要该字段的值,因此您进行了keyup事件。 (And not an other field, if there would be 2 fields with the same .user_id class.) (如果有两个具有相同.user_id类的字段,则不是另一个字段。)

尝试在焦点事件上绑定功能

$("input[submit]").click(function(e){
    e.preventDefault();
    var valid = validateUserId($('.user_id')); // maybe the function could return a boolean value too instead of just adding classes to the HTML part
    if (valid) {
        $("#your_form_id").submit(); // only if the input is valid, submit it
    }
});

sidenote on your problem: if you click the submit button it will first trigger the submit action which may give you an error and then execute the blur() block of code 关于问题的旁注:如果单击提交按钮,它将首先触发提交操作,这可能会给您带来错误, 然后执行代码的blur()

Here is a working demo http://jsfiddle.net/pomeh/GwswM/ . 这是一个有效的演示http://jsfiddle.net/pomeh/GwswM/ I've rewritten a little the code to: 我将一些代码重写为:

  • cache DOM selections into variables, 将DOM选择缓存到变量中,
  • use jQuery methods chaining, 使用jQuery方法链接
  • improve if conditions with tripe equal signs, if肚的符号相等,则改善
  • separated AJAX calls and application logic, 分离的AJAX调用和应用程序逻辑,
  • cache identical AJAX calls, 缓存相同的AJAX调用,
  • use jQuery deferred to hide the asynchronous aspect of the verification (linked to AJAX) 使用deferred jQuery隐藏验证的异步方面(链接到AJAX)

Hope that'll help :) 希望能对您有所帮助:)

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

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