简体   繁体   English

如何使用 $(document).ready(function() 和另一个事件 Function 运行 Javascript/Jquery Function?

[英]How to run a Javascript/Jquery Function using both $(document).ready(function() and Another Event Function?

I'm not a javascript/jquery coder, and not sure if what I'm trying to do is possible.我不是 javascript/jquery 编码器,并且不确定我想要做的事情是否可行。

I have a html/php/ajax form that is updated an sql database as the user fills it out.我有一个 html/php/ajax 表单,它在用户填写时更新了 sql 数据库。 As they fill the form, there is a progress bar ran by javascript/jquery that updates as the user types in the input.当他们填写表格时,有一个由 javascript/jquery 运行的进度条,它会随着用户输入的内容而更新。 The start of the function looks like this: function 的开头如下所示:

$("#update input").keyup(function() {

This works great.这很好用。 My problem is when the page is reloaded.我的问题是页面重新加载时。 My code is pulling sql data from the database to fill the value of every input on the page that has a value so that a user can come back and completely the form later.我的代码正在从数据库中提取 sql 数据,以填充页面上每个输入的值,以便用户稍后可以返回并完整填写表单。 When the user reloads the page, the only way for the script to activate is if the user types in an input field.当用户重新加载页面时,激活脚本的唯一方法是用户在输入字段中键入内容。

I thought I would fix the issue by changing the my initial javascript/jquery function with $(document).ready(function() . This caused the script to only run when the page was loaded and not when the form was being filled out. I need both the script to run on page ready, and when a user is typing in the input filled. Is there a way I can run both $(document).ready(function() AND $("#update input").keyup(function() { simultaneously? Or is there a better why to accomplish this? Thanks!我想我可以通过使用$(document).ready(function()更改我的初始 javascript/jquery function 来解决这个问题。这导致脚本仅在页面加载时运行,而不是在填写表单时运行。我需要脚本在准备好的页面上运行,并且当用户输入填充的输入时。有没有一种方法可以同时运行$(document).ready(function()$("#update input").keyup(function() {同时?或者有更好的为什么要做到这一点?谢谢!

Let me know if I need to post more code.如果我需要发布更多代码,请告诉我。

Here's a generic approach attaching declared functions to events.这是一种将声明的函数附加到事件的通用方法。

function handler (e) {}
element.addEventListener('click', handler);

You're free to call handler everywhere, also inside $(document).ready , or if there's no other code in your DOMReady handler, you can just pass a reference as an argument:您可以在任何地方调用handler ,也可以在$(document).ready内部调用,或者如果您的 DOMReady 处理程序中没有其他代码,您可以将引用作为参数传递:

$(document).ready(handler);

In your specific case you most likely want something like this:在您的特定情况下,您很可能想要这样的东西:

$(document).ready(function () {
  function handler (e) {...}
  handler();
  $("#update input").keyup(handler);
});

If the handler function uses the event object ( e in the example), in modern browsers it's also available as a global event object, or in jQuery, e.originalEvent . If the handler function uses the event object ( e in the example), in modern browsers it's also available as a global event object, or in jQuery, e.originalEvent . The object doesn't exist if there's no event fired, though, in that case you've to pass a fake event object, containing the provided properties, to the handler, if it is needed.如果没有触发事件,则 object 不存在,但是,在这种情况下,如果需要,您必须将包含提供的属性的假事件 object 传递给处理程序。

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

相关问题 jQuery / Javascript函数同时需要$(document).ready和$(document).ajaxSucces - JQuery / Javascript function requies both $(document).ready and $(document).ajaxSucces 如何从$(document).ready()运行javascript函数 - How to run javascript function from $(document).ready() JS / jQuery - 最好在$(document).ready或被调用函数中运行事件处理程序 - JS/jQuery - Better to run event handler in $(document).ready or in called function jQuery / Javascript-在(文档)之前运行功能。准备好加载图像 - jQuery/Javascript - Run function before (document).ready for the purpose of loading an image 如何在jQuery中的文档就绪事件上自动调用函数 - How to call a function automatically on document ready event in jQuery 带有$(document).ready(function()的Javascript - Javascript with $(document).ready(function() javascript文档准备功能 - javascript document ready function 使用$ {document).ready(function()尝试运行此jquery / javascript代码的页面将无法加载 - Page won't load trying to run this jquery/javascript code using $(document).ready(function() 如何按需从JavaScript而不是(document).ready运行jQuery函数? - How do I run a jQuery function on demand from JavaScript and not on the (document).ready? 在另一个jquery或javascript函数内调用jquery document.ready函数 - call jquery document.ready function inside another jquery or javascript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM