javascript 如何不在 <form> 中发送输入字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15670521/
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
How to not send an input field in a <form>?
提问by Kurt
Relating to my last question. I have an upload field where the user can choose a picture and this becomes resized (client-side via JavaScript), base64 encoded (JavaScript as well) and sent via a hidden field. I do this in order to save bandwidth of the user (e.g. usage with a 3G connection).
关于我的最后一个问题。我有一个上传字段,用户可以在其中选择图片,然后调整大小(客户端通过 JavaScript)、base64 编码(JavaScript 也是如此)并通过隐藏字段发送。我这样做是为了节省用户的带宽(例如使用 3G 连接)。
But I don't know how to not send the user upload file <input type="file" name="file" id="file" class="span4">
within the <form>
tags. The obvious solution would be to exclude the file upload field from the form but this would kill my layout. Is this possible?
但我不知道如何不在标签<input type="file" name="file" id="file" class="span4">
内发送用户上传文件<form>
。显而易见的解决方案是从表单中排除文件上传字段,但这会破坏我的布局。这可能吗?
回答by VALIKHAN
Just delete name="file"
attribute.
只需删除name="file"
属性。
回答by C???
You can do the following with jQuery to disable your input field:
您可以使用 jQuery 执行以下操作以禁用输入字段:
$('#file').prop('disabled', true);
Altogether you might have this:
总之你可能有这个:
// domReady handler
$(function() {
// provide an event for when the form is submitted
$('#myform').submit(function() {
// Find the input with id "file" in the context of
// the form (hence the second "this" parameter) and
// set it to be disabled
$('#file', this).prop('disabled', true);
// return true to allow the form to submit
return true;
});
});
回答by darwin
If you can add the attribute "disabled" to the input, it won't be submitted along with the form:
如果您可以将“禁用”属性添加到输入中,它将不会与表单一起提交:
<input type="file" name="file" id="file" class="span4" disabled="disabled">
You can set this attribute in your js-script...
您可以在 js 脚本中设置此属性...
回答by Alysson Myller
I needed the form and the input to have a name tagso what i have to do was to set a method tagon my form. This way, my inputs can be enabled and have a tag name.
我需要表单和输入有一个名称标签,所以我要做的是在我的表单上设置一个方法标签。这样,我的输入可以被启用并有一个标签名称。
<form name="form" method="post">