我们在特定的页面上有链接,这些链接是动态提供的。
假设我们在一个关于苹果的页面。
其中一个链接可以是,搜索苹果。
所以非常基本。
<a href="goto.php?q=apples">Search for Apples</a>
在goto.php页面上,我们实际上有一个ajax搜索字段,当用户输入时,它会使用ajax ping我们的数据库,并显示下面的结果。
其输入字段为:
<input name="suburbs" autocomplete="off" class="suburbsreviews" type="text" id="terms" size="50" onkeyup="getScriptPage('show_results','terms','1')" onblur="if ( this.value == '' ) this.value = this.defaultValue" onfocus="if ( this.value == this.defaultValue ) this.value = ''" value="type your search term" />
实际上,我要做的是读取传入的链接搜索词,在本例中是apple,并将其添加到输入字段中。因此,在页面加载时,输入字段包含单词apple ..
我担心的是输入字段使用ajax工作,并在keyup上触发请求,所以只是将单词apple作为值添加到输入框中,并不是真正的工作>
还是
我不这么认为,所以想知道是否有人知道我们可以启动这个工作的方式,以便输入元素包含引用页面搜索词,并触发ajax请求,以便结果显示在它下面…
ajax页面完全工作,如果我们键入单词,因为它监听keyup事件,但想知道是否有一个解决方案…
欢呼Ste
如果文本框有值,您是否尝试在页面加载时触发keyup事件?
if( $('#yourinput').val() !== '' )
$('#yourinput').trigger('keyup')
p。我不太喜欢这个解决方案。
如果字段不为空,则可以在页面加载时启动keyup因为您没有jquery,所以让我们使用纯javascript。
这应该在你的body中onload:
var val = document.getElementById('terms').value;
if(val.length > 0 && val != 'placeholder'){
getScriptPage('show_results','terms','1');
}
将搜索词作为<input>
的值,并在goto.php
中添加一段Javascript,当页面加载时(如果输入字段不为空)调用一次keyup处理程序
查看这些站点实际获取URL参数:
使用JS和/或jQuery使用JS和正则表达式
现在将值插入输入字段:
var searchTerm = 'mySearchTerm'; // see above to get the search term from the url
$("input[name='suburbs']").val(searchTerm);
现在调用完成自动补全的函数。放在一起,它看起来像这样:
var searchTerm = 'mySearchTerm'; // see above to get the search term from the url
$("input[name='suburbs']").val(searchTerm);
getScriptPage('show_results','terms','1');