javascript 不使用 <input type="text"> 创建输入文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8111958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a input textbox without using <input type="text">
提问by Nyxynyx
I want to create a input textbox that does not require a <input type="text">
form element. The user can still type text into this textbox and the value can still be retrieved using Javascript/jQuery. The reason for doing this is because this textbox will be in a form that upon submit, all form input values will be sent to the server side for processing, and there is this textbox that I dont want its values to be automatically send to server side.
我想创建一个不需要<input type="text">
表单元素的输入文本框。用户仍然可以在此文本框中键入文本,并且仍然可以使用 Javascript/jQuery 检索该值。这样做的原因是因为这个文本框将在一个表单中,在提交时,所有表单输入值将被发送到服务器端进行处理,并且有这个文本框我不希望它的值自动发送到服务器端.
Problem:What is the best way to create such a textbox without relying on HTML form elements input
?
问题:在不依赖 HTML 表单元素的情况下创建这样的文本框的最佳方法是什么input
?
回答by Asaph
Just don't give the <input type="text">
tag a name
attribute and it won't submit. Here is an example:
只是不要给<input type="text">
标签一个name
属性,它不会提交。下面是一个例子:
<input type="text" id="myinput" value="you will not see this submitted" />
回答by Philippe Plantier
You could use a contenteditable div, too.
您也可以使用 contenteditable div。
From the MDN:
来自 MDN:
<!DOCTYPE html>
<html>
<body>
<div contenteditable="true">
This text can be edited by the user.
</div>
</body>
</html>
回答by Royi Namir
if you create dynamic <input type="text">
by jQuery - it WILL post back its values.
如果您<input type="text">
通过 jQuery创建动态- 它会回发其值。
What do you care if its values are posted back ? just dont do anything with them in the server side.
如果它的值被回传,你关心什么?只是不要在服务器端对它们做任何事情。
or ,
或者 ,
before submit - change the type from input to label. ( + save values)
提交之前 - 将类型从输入更改为标签。( + 保存值)
or ,
或者 ,
if youll add the input OUTSIDE the FORM element - it wont submit.
如果您将输入添加到 FORM 元素之外 - 它不会提交。
回答by tmaximini
you can use a textarea
instead of input
. then apply stlying with css. also you can use your input tag, just keep it outside the <form></form>
then it won't be send over to the server.
您可以使用 atextarea
代替input
. 然后用css应用stlying。你也可以使用你的输入标签,只要把它放在外面,<form></form>
然后它就不会被发送到服务器。
回答by Kae Verens
just create the input, and don't give it a name
or id
.
只需创建输入,不要给它一个name
或id
。
回答by Tim Joyce
$("#form").bind('submit', function() {
$(this).children('input[name=excludethisfield]').attr('disabled', true);
});
I know this isn't specifically what you asked but might be the easiest way.
我知道这不是你问的具体问题,但可能是最简单的方法。