自定义CSS输入框导致页面在ENTER键上刷新

时间:2012-07-18 04:43:35

标签: javascript html css

  

可能重复:
  Stop Enter/Return key submitting a form

我正在尝试使用我在网上找到的css创建一个简单的搜索表单。我希望用户能够从文本框中点击ENTER键并使其运行查询而无需用户单击按钮。我尝试在keydown上检查ENTER键,但页面由于某种原因保持刷新。这次刷新的原因是什么?

.searchform {
    display: inline-block;
    zoom: 1; /* ie7 hack for display:inline-block */
    *display: inline;
    border: solid 1px #d2d2d2;
    padding: 3px 5px;

    -webkit-border-radius: 2em;
    -moz-border-radius: 2em;
    border-radius: 2em;

    -webkit-box-shadow: 0 1px 0px rgba(0,0,0,.1);
    -moz-box-shadow: 0 1px 0px rgba(0,0,0,.1);
    box-shadow: 0 1px 0px rgba(0,0,0,.1);

    background: #f1f1f1;
    background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#ededed));
    background: -moz-linear-gradient(top,  #fff,  #ededed);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed'); /* ie7 */
    -ms-filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed'); /* ie8 */
}
.searchform input {
    font: normal 12px/100% Arial, Helvetica, sans-serif;
}
.searchform .searchfield {
    background: #fff;
    padding: 6px 6px 6px 8px;
    width: 202px;
    border: solid 1px #bcbbbb;
    outline: none;

    -webkit-border-radius: 2em;
    -moz-border-radius: 2em;
    border-radius: 2em;

    -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,.2);
    -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.2);
    box-shadow: inset 0 1px 2px rgba(0,0,0,.2);
}
.searchform .searchbutton {
    color: #fff;
    border: solid 1px #494949;
    font-size: 11px;
    height: 27px;
    width: 35px;
    text-shadow: 0 1px 1px rgba(0,0,0,.6);

    -webkit-border-radius: 2em;
    -moz-border-radius: 2em;
    border-radius: 2em;

    background: #5f5f5f;
    background: -webkit-gradient(linear, left top, left bottom, from(#9e9e9e), to(#454545));
    background: -moz-linear-gradient(top,  #9e9e9e,  #454545);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#9e9e9e', endColorstr='#454545'); /* ie7 */
    -ms-filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#9e9e9e', endColorstr='#454545'); /* ie8 */
}

#search {margin-top:30px; marin-right:auto;}

使用以下表格:

<div align="center" id="search">
    <form class="searchform">
        <input class="searchfield" style="color:#636363" type="text" value="Title/Album..." onkeydown="if (event.keyCode == 13) {querySong();}" onfocus="if (this.value == 'Title/Album...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Title/Album...';}" />
    </form>
    <form class="searchform">
        <input class="searchfield" style="color:#636363" type="text" value="Artist..." onkeydown="if (event.keyCode == 13) {querySong();}" onfocus="if (this.value == 'Artist...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Artist...';}" />
        <input class="searchbutton" type="button" value="Song" onclick="querySong()"/>&nbsp
        <input class="searchbutton" type="button" value="Album" onclick="queryAlbum()"/>
    </form>
</div>

1 个答案:

答案 0 :(得分:2)

您需要阻止表单在Enter上执行默认操作,即提交表单。

这有点沉重,您需要为您的案例选择合适的表单,但基本上您只需要返回false以防止默认行为。

$('form').submit( function() { return false; } );

或没有jQuery

var el = document.getElementById('formId');

if (el.addEventListener) {
  el.addEventListener('submit', preventDefaultAction, false); 
} else if (el.attachEvent)  {
  el.attachEvent('submit', preventDefaultAction);
}

var preventDefaultAction = function () { return false; }