jquery 在加载时触发所有输入元素模糊

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10946667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:00:35  来源:igfitidea点击:

jquery trigger all input elements with blur on load

jqueryeventstriggerscopy

提问by ThomasK

I'm using blur()to copy what the user writes in a form into a summary page at the end of a registration wizard. And this is working great.

我正在使用blur()将用户在表单中写入的内容复制到注册向导结束时的摘要页面中。这很好用。

But when I preset some field values and these are correct, nothing is copied since the user probably don't gonna interacted with that particular filed. They'll just gonna click continue.

但是当我预设一些字段值并且这些值是正确的时,不会复制任何内容,因为用户可能不会与该特定字段进行交互。他们只会点击继续。

Is there a way to trigger all text fields, textareas in order get those values copied aswell?

有没有办法触发所有文本字段和文本区域,以便同时复制这些值?

This is the function I'm using:

这是我正在使用的功能:

/**
 *  Author: Thomas Kile
 *  Desc:   Copy text from a form element into a given tag.
 **
 *  @param string $type type of form element
 *  @param string $from Id of form element to copy text/value from.
 *  @param string $to Id of element to copy text/value into.
 */
    function copyFormData(type,from,to)
    {   
        switch (type)
        {
            case 'text':  var copied_text = $(from).val();  break; //  get input text value
            case 'select': var copied_text = $(from+' option:selected').text();  break;
        }
        $(to).text(copied_text);   //  put inside this tag
    }

And this is how I'm using it:

这就是我使用它的方式:

$(firstName).blur(function(){   copyFormData('text',firstName,'strong#firstName');  });
$(lastName).blur(function(){    copyFormData('text',lastName,'strong#lastName');    });

Where should I put a trigger()event? I used trigger()on a select>first option once the list was fetched with getJSON in order to populate next list automatically in a chained select thing. But that was a bit different...

我应该在哪里放置trigger()事件?使用getJSON 获取列表后,我在 select>first 选项上使用了trigger(),以便在链式选择事物中自动填充下一个列表。但这有点不同......

回答by Sergei Zahharenko

You can use trick :)

你可以使用技巧:)

$('input').each(function(){
  $(this).trigger('blur');
  //each input event one by one... will be blured
})

回答by Razor

$('input[type=text], textarea').blur();

Or (potentially faster if everything is bound with jQuery):

或者(如果一切都与 jQuery 绑定,可能会更快):

$('input[type=text], textarea').triggerHandler('blur');

回答by Ihor Deyneka

Have you tried .trigger() ?

你试过 .trigger() 吗?

http://api.jquery.com/trigger/

http://api.jquery.com/trigger/

回答by Jeff Watkins

How about, taking your copy input to summary code and putting it into a function. keep the blur as it is (except calling this function rather than inline code) and on $(document).ready() select all of your text boxes and copy them over if they contain anything. Simpler than trying to fudge events.

怎么样,将您的副本输入到摘要代码中并将其放入一个函数中。保持模糊(除了调用此函数而不是内联代码)并在 $(document).ready() 上选择所有文本框并复制它们,如果它们包含任何内容。比试图捏造事件更简单。

回答by Petros Mastrantonas

you could try to trigger them programmatically by firing up

您可以尝试通过启动以编程方式触发它们

$("selector").trigger("blur"); 

api.jquery.com/trigger is your friend :-)

api.jquery.com/trigger 是你的朋友 :-)

回答by user3361464

You can use bellow code it will help you

您可以使用以下代码,它会帮助您

 $('input:text').each(function(){
        $(this).trigger('blur');
    })

回答by Neel

If you happened to add the same prefix to all IDs of the input and wish to only blur these inputs all at once, you can use the wildcard selection and do it like this:

如果您碰巧为输入的所有 ID 添加了相同的前缀,并且希望一次只模糊这些输入,您可以使用通配符选择并这样做:

$("[id^=inputbox] input").trigger("blur");