[英]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.