将div附加到搜索文本框以创建类似于搜索框的Gmail

时间:2013-03-08 23:35:36

标签: javascript jquery twitter-bootstrap search-box

我正在创建一个类似Gmail的搜索框,我在其中单击文本框,它会打开一个新的div,我可以在其中输入各种字段的搜索条件。 如何将TextBox('#searchTextBox)或包含文本框的div紧密附加到搜索表单的div('#gmailSearchBox)? 此外,我使用bootstrap响应,因此文本框的位置和大小更改。所以只需设置div样式显示:块和显示:none不起作用。 感谢。

Html代码:

<div style="border:1px solid grey; border-radius:3px; max-width:300px; padding-right:0px; padding-left:0px;"class="container-fluid">
    <div style="left:0px;right:20px; line-height:inherit; float:left; width:100%; ">
        <input id="searchTextBox" type="text" style="border:none; width:95%; line-height:inherit; padding:0px 0px 0px 0px;"> 
        <a id="showSearchBox" href="#" style="height:20px">
             <span class="caret" style="vertical-align:middle; height:100%"> </span>
        </a>
    </input>
</div>
</div>
<input type="button" value="Search" Class="btn btn-primary" /><br>
Various<br>
Other<br>
Information<br>
Goes <br>
Here<br>
<div id='gmailSearchBox' class="gmailSearchBox" style="position:absolute; display:none; border:1px solid grey">
<form action="" method="post">
    From:<input type="text" id="fromDate">
    <br/>
    To: <input type="text" id="toDate">
    <br/>
    Type:<select id="logType">
            <option>--Select Type--</option>
        </select>
    <br/>
    Text:<input type="text" id="searchText">
    <br/>
    <input type="submit" class="btn btn-primary">
</form>
</div>

Javascript代码

$('#showSearchBox').click(showSearchBox);
function showSearchBox() {
    $('#gmailSearchBox').css('display','block')
}

JsFiddle链接 - http://jsfiddle.net/7FBax/

2 个答案:

答案 0 :(得分:1)

如果你想用css做这个,你可能需要稍微改变你的标记。请参阅以下小提琴,它可以满足您的需求:http://jsfiddle.net/97BwC/1/

修订后的标记:

<div id="search" style=""class="container-fluid">
<div id="searchInputWrapper" style="">
    <input type="text" style=""> <a id="showSearchBox" href="#" style="height:20px">
             <span class="caret" style="vertical-align:middle; height:100%"> </span>
        </a>

    </input>
</div>
<div id='gmailSearchBox' class="gmailSearchBox" style="">
    <form action="" method="post">From:
        <input type="text" id="fromDate">
        <br/>To:
        <input type="text" id="toDate">
        <br/>Type:
        <select id="logType">
            <option>--Select Type--</option>
        </select>
        <br/>Text:
        <input type="text" id="searchText">
        <br/>
        <input type="submit" class="btn btn-primary">
    </form>
</div>

css:

    #search {
        position:relative;
        border:1px solid grey;
        border-radius:3px;
        max-width:300px;
        padding-right:0px;
        padding-left:0px;
    }
    #searchInputWrapper {
        left:0px;
        right:20px;
        line-height:inherit;
        float:left;
        width:100%;
    }
    #searchInputWrapper input {
        border:none;
        width:95%;
        line-height:inherit;
        padding:0px 0px 0px 0px;
    }
    #gmailSearchBox {
        position:absolute;
        bottom:-212px;
        right:0;
        display:none;
        border:1px solid grey;
        background-color:#fff;
    }

<强>更新

如果搜索框的高度不是静态的,您可以使用javascript设置底部偏移量。这是一个更新的小提琴:http://jsfiddle.net/GPTJ4/2/

这是经过修改的处理程序:

$('#showSearchBox').click(showSearchBox);

function showSearchBox() {
    var height = 0,
        $searchBox = $('#gmailSearchBox');

    $searchBox.css({display:'block'});

    //get the height of the search box
    height = $searchBox.height();

    //set the offset equal to the height
    $searchBox.css({bottom:-height +'px'});
}

答案 1 :(得分:0)

如果您将#gmailSearchBox放在包含输入的同一div中,它将按预期工作。您可能还想为#gmailSearchBox设置背景颜色。

相关问题