简体   繁体   English

输入文本后如何启动JQuery / ajax操作-Ruby on Rails

[英]How to launch JQuery/ajax action after text input - Ruby on Rails

I want to be able to enter a URL into a form, without clicking submit, for the page to auto-process the URL (processing take a 3-5 seconds) and then return some information about the webpage (like title, images, etc). 我希望能够在不单击提交的情况下将URL输入到表单中,以便页面自动处理URL(处理需要3-5秒),然后返回有关网页的某些信息(例如标题,图像等) )。 For example, the code would know when the user stopped typing information and then auto-process the text. 例如,代码将知道用户何时停止键入信息,然后自动处理文本。

I'm using Rails 3.1. 我正在使用Rails 3.1。 Should I have a polling function that polls 3 or 4 times per second to accept the URL? 我是否应该具有每秒轮询3或4次以接受URL的轮询功能? I want this site to be used by thousands of users at a time so I hope that's not too much overhead. 我希望这个网站一次可以被成千上万的用户使用,所以我希望这不会带来太多开销。 And I worry that if a user manually enters the URL (as opposed a quick copy and paste), the auto-processor would process the URL WHILE it is still being entered. 而且我担心如果用户手动输入URL(而不是快速复制和粘贴),则自动处理器会处理仍在输入的URL。

While the processing is happening, I plan to use a 'loading' image. 在处理过程中,我计划使用“加载”图像。 I feel like this has parallels to text auto-complete. 我觉得这与自动完成功能类似。

why don't you use something like . 你为什么不使用这样的东西? mousekeyclick+ keydown.. would work whether its copy paste or typing.. Also sending request on each key down is bad IMHO. mousekeyclick + keydown ..不管复制粘贴还是键入都可以使用。同样在每个按键上发送请求都是不好的恕我直言。

What you can do is set time out function of 0.8 seconds ( can vary depending on your requirement) after mouse/key is pressed. 您可以做的是在按下鼠标/键后将超时功能设置为0.8秒(可以根据您的要求而定)。 so within this duration if a key is not pressed then you make the request , if a key is pressed within this duration you cancel out your previous timeout function :) 因此,在此持续时间内,如果未按下任何键,则发出请求;如果在此持续时间内按下某个键,则取消先前的超时功能:)

why don't you consider the 'Enter' key on onkeyPress or onkeydown event of your textbox. 为什么不考虑文本框的onkeyPress或onkeydown事件上的“ Enter”键。 You can give the instruction to your users to "write the url and press the 'Enter' key to process the request." 您可以向用户指示“编写URL并按Enter键以处理请求”。 with a label. 带标签。 Also you can use the settimeout method of javascript to process the requests after some interval of time. 您也可以使用javascript的settimeout方法在一段时间后处理请求。

For Eaxmple; 对于Eaxmple;

<input type="text" onkeypress="ProcessRequest(event)" />

and your javascript would be like this.. 和您的JavaScript将是这样的..

 <script type="text/javascript">
    function ProcessRequest(e)
    {
       if(e.keyCode == 13)// 13 is the key code of 'Enter' key
       {
           var t=setTimeout("ProcessURL()",4000);
       }
       else 
           return false;
    }
    function ProcessURL()
    {
      // your code to submit url
    }
    </script>

暂无
暂无

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

相关问题 如何在使用jQuery / AJAX的Ruby on Rails中的其他模型视图中使用其他控制器动作? - How do I use another controllers action in the view of a different model in Ruby on Rails using jQuery/AJAX? 如何通过jQuery在Ruby on Rails中实现Ajax - how to implement ajax in Ruby on rails via jquery 在 ruby-on-rails 应用程序上提交带有 AJAX 的表单后,如何让 jQuery 更新页面上的元素? - How can i get jQuery to update an element on a page after submitting a form with AJAX on a ruby-on-rails app? 在Ruby On Rails中插入注释ajax之后如何通过ajax更新分页 - How to update pagination by ajax after insert comment ajax in Ruby On Rails Ruby On Rails jQuery AJAX Post - Ruby On Rails jQuery AJAX Post 使用Ajax Rails保存后的操作4 - Action after save with Ajax Rails 4 Ruby / Rails / AJAX / JQuery-在链接单击时,传递参数并执行控制器操作 - Ruby/Rails/AJAX/JQuery - On link click, passing a parameter and performing a controller action 如何在Rails的TimePicker jQuery ruby​​的Ajax函数中访问变量 - How to access variable in an ajax function for timepicker jquery ruby on rails 如何使用jQuery AJAX请求和Ruby on Rails&#39;render&#39;方法来实现? - How to use the jQuery AJAX request and the Ruby on Rails 'render' method togheter? (ruby on rails)服务器数据更新后,如何使用jquery / ajax以最快的方式从部分加载/显示特定div? - (ruby on rails)After an server data update, how to load/display a specific div from a partial in the quickest way with jquery/ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM