简体   繁体   English

如何更改PHP搜索表单中的值(焦点/模糊)?

[英]How to change the value in a PHP search form (on focus / on blur)?

In a search form I want to change the initial value in the search field from 'Your Search' to ' ' (none) as the user enters the search field (on focus) and to change it back to the original value when the user leaves the field without entering a search (on blur). 在搜索表单中,我想在用户进入搜索字段(焦点上)时将搜索字段中的初始值从“您的搜索”更改为“无(无)”,并在用户离开时将其更改回原始值没有输入搜索的字段(模糊)。 The Boilerplate function I am using is this: 我正在使用的Boilerplate功能是这样的:

    function boilerplate_search_form ( $form ) {
    $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
    <div>
    <label class="screen-reader-text" for="s">' . __('Search for:','luchtspin') . '</label>
    <input type="search" placeholder="' . __('', 'luchtspin') . '" value="'. __('Your search','luchtspin') .'" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search','luchtspin') .'" />
    </div>
    </form>';
    return $form
    }
    add_filter( 'get_search_form', 'boilerplate_search_form' );

I was thinking along the lines of adding the code below, but obviously I can't just throw in an if statement and that is exactly where my PHP skills stop. 我正在考虑添加下面的代码,但显然我不能只是抛出一个if语句,这正是我的PHP技能停止的地方。

    onfocus=if (form.value == "'. __('Your search','luchtspin') .'") {form.value = '';} onblur=if (form.value == '') {form.value = "'. __('Your search','luchtspin') .'";}

This is probably not too difficult. 这可能不是太困难。 For good measure a link to the site with the search form: www.luchtspin.nl. 为了更好地衡量与搜索表单的链接:www.luchtspin.nl。 Anyone who can assist me with this? 谁可以帮助我这个?

Just use the HTML5 placeholder attribute. 只需使用HTML5 placeholder属性即可。 <input type="text" name="query" placeholder="Your Search">

You can use jQuery for this task 您可以使用jQuery执行此任务

$("input").blur(function() {
    if($.trim($(this).val()).length == 0)
        $(this).val($(this).data('last_value'));
}).focus(function() {
    $(this).data('last_value', $(this).val());
    $(this).val("")
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM