为什么在输入格式后附加“?"?什么时候按下回车键? [英] Why is input form appending "?" when enter key is pressed?

查看:85
本文介绍了为什么在输入格式后附加“?"?什么时候按下回车键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站位于此处:在搜索输入表单中输入文字时,页面会刷新并附加?"网址.

My site is here: When entering text in the search input form the page refreshes and appends "?" to the url.

但是,当按下搜索链接时,一切正常.关于为什么要加上?",这真让我感到困惑.因为我没有在任何地方包含该代码.预期的行为是单击链接而不是刷新页面.

However, when the search link is pressed, everything works fine. Its really boggling my mind as to why it is appending "?" as I haven't included that code anywhere. The intended behavior is for it to click the link instead of refreshing the page.

$(document).ready(function() {
    $("#coolIrisSearch").keydown(function(e) {
        if(e.keyCode === 13) {
            $("#searchbutton").click();
            return false;
        }
    });

    doCoolIrisSearch = function() {
        cooliris.embed.setFeedURL( 'http%3A%2F%2Fpipes.yahooapis.com%2Fpipes%2Fpipe.run%3FSearch%3D'+encodeURIComponent($('#coolIrisSearch').val())+'%26_id%3D5f4545ce4062c36e4c5d9a8763b3167e%26_render%3Drss' );
        filter();
    };   
}); 

确定了答案.表单字段非常严格.您无法删除提交"输入字段并将其更改为链接.否则,输出将执行以前的操作-附加?"以及第一个输入字段的名称属性.这是正确的版本:

Ok found the answer. The form field is pretty strict. You can't remove the "submit" input field and change it into a link. Or else, the output will do what it was doing previously - appending "?" and the name attribute of the first input field. This is the correct version:

    <form name="searchForm">
    <input type="text" name="coolIrisSearch" id="coolIrisSearch" onfocus="this.value=''" />
    <input type="submit" value="search" onclick="doCoolIrisSearch();return false;" />
    </form>

尽管此版本将导致url附加?coolIrisSearch =并重新加载页面.

While this version will cause the url to append "?coolIrisSearch=" and reload the page.

<form name="searchForm">
<input type="text" name="coolIrisSearch" id="coolIrisSearch" onfocus="this.value=''" />
</form>

希望其他人对此有帮助.

Hope someone else finds this helpful.

推荐答案

与其在按钮上添加点击侦听器,在输入中添加关键内容,不如尝试监听表单的submit事件.

Instead of adding click listeners to buttons and key things to inputs, try just listening to the submit event of forms.

$("#searchForm").submit(function() {
    doCoolIrisSearch();
    return false;
});

简单得多.

编辑:我尝试使用Chrome出色的Web检查器调试代码,看来您正在调用filter,有时带有id,有时没有id.您可以通过更改以下行来解决此问题:

I tried debugging your code using Chrome's excellent Web Inspector and it appears you're calling filter with an id sometimes, and without an id sometimes. You can fix this by changing this line:

document.getElementById(id).className += " active";

对此:

if(id) {
    document.getElementById(id).className += " active";
}

这篇关于为什么在输入格式后附加“?"?什么时候按下回车键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆