Drupal - 搜索框不起作用 - 自定义主题模板
我正在使用 search-theme-form.tpl 的自定义版本 当我使用搜索框时,我确实会转到搜索页面。但搜索实际上并没有发生。不过,搜索结果页面上的搜索框确实有效。这是我的 search-them-form.tpl.php 文件(演示:
<input type="text" name="search_theme_form_keys" id="edit-search-theme-form-keys" value="Search" title="Enter the terms you wish to search for" class="logininput" height="24px" onblur="restoreSearch(this)" onfocus="clearInput(this)" />
<input type="submit" name="op" id="edit-submit" value="" class="form-submit" style="display: none;" />
<input type="hidden" name="form_token" id="edit-search-theme-form-form-token" value="<?php print drupal_get_token('search_theme_form'); ?>" />
<input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form" />
还有我想从代码中可以清楚地看出它的用途:
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
function clearInput(e) {
e.value=""; // clear default text when clicked
e.className="longininput_onfocus"; //change class
}
function restoreSearch(e) {
if (trim(e.value) == '') {
{
e.value="Search"; // reset default text onBlur
e.className="logininput"; //reset class
}
}
}
问题是什么以及如何解决它?
I am using a customised version of search-theme-form.tpl
When I use the search box, I do get transferred to the search page. But the search does not actually take place. The search box on the search results page does work though. This is my search-them-form.tpl.php file (demo :
<input type="text" name="search_theme_form_keys" id="edit-search-theme-form-keys" value="Search" title="Enter the terms you wish to search for" class="logininput" height="24px" onblur="restoreSearch(this)" onfocus="clearInput(this)" />
<input type="submit" name="op" id="edit-submit" value="" class="form-submit" style="display: none;" />
<input type="hidden" name="form_token" id="edit-search-theme-form-form-token" value="<?php print drupal_get_token('search_theme_form'); ?>" />
<input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form" />
There is also a javascript file involved. I guess it's use is pretty clear from the code:
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
function clearInput(e) {
e.value=""; // clear default text when clicked
e.className="longininput_onfocus"; //change class
}
function restoreSearch(e) {
if (trim(e.value) == '') {
{
e.value="Search"; // reset default text onBlur
e.className="logininput"; //reset class
}
}
}
What can be the problem and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,您不能直接修改
search-theme-form.tpl.php
中的 HTML,因为这不是正确的方法。所以我添加类以及 onFocus 和 onBlur 属性就是问题所在。正确的方法是修改主题
template.php
文件。基本上我们将使用 form_alter() 来修改表单元素。由于使用HTML方式是错误的。看一下下面的代码(取自:此处)在
yourthemename_preprocess_search_theme_form
中 - “yourthemename”显然会反映您的自定义主题的名称。基本上代码是不言自明的。评论什么的。所以,基本上这就是它的工作方式。
Apparently, you cannot directly modify the HTML in
search-theme-form.tpl.php
since thats not the right way to do it. So my adding the class and onFocus and onBlur attributes was the problem.The correct way to do it is to modify the themes
template.php
file. Basically we will be using form_alter() to modify the form elements. Since using the HTML way is wrong. Take a look at the code below (taken from : here )In
yourthemename_preprocess_search_theme_form
- 'yourthemename' will obviously reflect the name of your custom theme. Basically the code is self-explanatory. what with the comments and all.So, basically thats the way it works.