简体   繁体   English

javascript在按下按钮或返回键时从输入转到URL

[英]javascript go to URL from input on button press or return key

I have a page with an input field where I'd like to send the user to the URL they enter in the input, with -.html appended at the end.我有一个带有输入字段的页面,我想将用户发送到他们在输入中输入的 URL,并在末尾附加 -.html。 My HTML is as follows:我的 HTML 如下:

<form name="codeform"> 
<input type="text" id="code" name="code" autofocus>
</form>

<a href="#" class="button" id="submit">SUBMIT</a>

And my javascript:还有我的 javascript:

$('#submit').click(function () { location.href = window.document.codeform.code.value + '.html'; });

When the user clicks the button, the script works as intended and the user goes to the page.当用户单击按钮时,脚本按预期工作并且用户转到该页面。 But I'd also like the script to execute when the return key is pressed.但我也希望在按下返回键时执行脚本。 Right now, when the return key is pressed, it tries to submit the form and I end up with a query string.现在,当按下返回键时,它会尝试提交表单,我最终得到一个查询字符串。

What is the best way to send the user to the page whether the button or return key is pressed?无论按下按钮还是返回键,将用户发送到页面的最佳方式是什么?

Because your a.button is outside from the form, you need to add trigger to the form and the a.button too.因为您的a.button在表单之外,所以您也需要向表单和a.button添加触发器。 Then just detect the Enter keycode , and done.然后只需检测Enter keycode ,就完成了。

 $('form').submit(function(e) { e.preventDefault(); console.log(window.document.codeform.code.value + '.html') }); $('form').keydown(function(e) { if (e.which == 13) { console.log(window.document.codeform.code.value + '.html') } }); $('#submit.button').click(function() { console.log(window.document.codeform.code.value + '.html') });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <form name="codeform" id="codeform"> <input type="text" id="code" name="code" autofocus> </form> <a href="#" class="button" id="submit">SUBMIT</a>

You can add a keydown listener that only runs on enter (keycode 13), and then prevents the default form submission action:您可以添加一个仅在输入时运行的 keydown 侦听器(键码 13),然后阻止默认的表单提交操作:

$('#submit').click(function () { location.href = window.document.codeform.code.value + '.html'; });
$('#code').keydown(function (event) {
    if(event.which==13){
        event.preventDefault();//prevents form submission
        location.href = window.document.codeform.code.value + '.html';
    }
});

Since you have the logic attached to the button click() event, you can simply attach a keypress() event to the input box to subsequently execute the button click.由于您将逻辑附加到按钮 click() 事件,您可以简单地将 keypress() 事件附加到输入框以随后执行按钮单击。

$("#code").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $('#submit').click();       
    }
});

However, for tidiness I usually prefer to move the logic from the click event into a separate function and call that one function from both the click() and keypress() events.但是,为了整洁,我通常更喜欢将逻辑从 click 事件移动到一个单独的函数中,并从 click() 和 keypress() 事件中调用该函数。

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

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