没有表单的HTML输入框

时间:2021-11-28 06:24:24

I need to have an input box in a div without a form and when the user enters something and hits return, it should run a Javascript function.

我需要在没有表单的div中有一个输入框,当用户输入内容并点击返回时,它应该运行Javascript函数。

There will be no submit button.

没有提交按钮。

How can I do this?

我怎样才能做到这一点?

5 个解决方案

#1


20  

To get an input box without a form, I would suggest just not using a form.

要获得没有表单的输入框,我建议不要使用表单。

#2


2  

You will have to attach a onkeypress event and check if enter was pressed (and rune the code if it was). Tell us if you are using plain JavaScript or some library if you need examples.

您将必须附加onkeypress事件并检查是否按下了输入(如果是,则检查代码)。如果您需要示例,请告诉我们您使用的是纯JavaScript还是某些库。

#3


1  

It can be simply done with the help of a form as follow... i think adding form would provide great stability

它可以简单地在一个表格的帮助下完成...我认为添加表单将提供很大的稳定性

<form onSubmit='alerttest(this); return false'>        
    <input type="text">         
</form>


<script language="javascript">
    function alerttest() {
        alert("Executing jolly.exe !!!!");
    }
</script>

#4


0  

Don't use the form tag. You can easily use JQuery to tie the code to the input control's key press events.

不要使用表单标记。您可以轻松使用JQuery将代码绑定到输入控件的按键事件。

If you are unfamiliar with JQuery, just tie the code directly to the input field key press event.

如果您不熟悉JQuery,只需将代码直接绑定到输入字段按键事件即可。

#5


0  

To run a function when the user presses enter when focused on the input field, the easiest way would be to have the form tag and run the function using the form's onSubmit event (because pressing return when focused on an input field submits the form).

要在用户在聚焦输入字段时按Enter键运行函数,最简单的方法是使用表单标签并使用表单的onSubmit事件运行函数(因为当聚焦在输入字段时按下返回提交表单)。

If you nevertheless don't want to include the form tag, you can use the onKeyPress event to catch the return key pressing and call the function from there.

如果您仍然不想包含表单标记,则可以使用onKeyPress事件来捕获返回键并从那里调用该函数。

#1


20  

To get an input box without a form, I would suggest just not using a form.

要获得没有表单的输入框,我建议不要使用表单。

#2


2  

You will have to attach a onkeypress event and check if enter was pressed (and rune the code if it was). Tell us if you are using plain JavaScript or some library if you need examples.

您将必须附加onkeypress事件并检查是否按下了输入(如果是,则检查代码)。如果您需要示例,请告诉我们您使用的是纯JavaScript还是某些库。

#3


1  

It can be simply done with the help of a form as follow... i think adding form would provide great stability

它可以简单地在一个表格的帮助下完成...我认为添加表单将提供很大的稳定性

<form onSubmit='alerttest(this); return false'>        
    <input type="text">         
</form>


<script language="javascript">
    function alerttest() {
        alert("Executing jolly.exe !!!!");
    }
</script>

#4


0  

Don't use the form tag. You can easily use JQuery to tie the code to the input control's key press events.

不要使用表单标记。您可以轻松使用JQuery将代码绑定到输入控件的按键事件。

If you are unfamiliar with JQuery, just tie the code directly to the input field key press event.

如果您不熟悉JQuery,只需将代码直接绑定到输入字段按键事件即可。

#5


0  

To run a function when the user presses enter when focused on the input field, the easiest way would be to have the form tag and run the function using the form's onSubmit event (because pressing return when focused on an input field submits the form).

要在用户在聚焦输入字段时按Enter键运行函数,最简单的方法是使用表单标签并使用表单的onSubmit事件运行函数(因为当聚焦在输入字段时按下返回提交表单)。

If you nevertheless don't want to include the form tag, you can use the onKeyPress event to catch the return key pressing and call the function from there.

如果您仍然不想包含表单标记,则可以使用onKeyPress事件来捕获返回键并从那里调用该函数。