【问题标题】:How to not send an input field in a <form>?如何不在 <form> 中发送输入字段?
【发布时间】:2023-03-26 16:13:01
【问题描述】:

与我的last question 有关。我有一个上传字段,用户可以在其中选择一张图片,它会调整大小(通过 JavaScript 客户端),base64 编码(JavaScript 也是)并通过隐藏字段发送。我这样做是为了节省用户的带宽(例如使用 3G 连接)。

但我不知道如何不在&lt;form&gt; 标签内发送用户上传文件&lt;input type="file" name="file" id="file" class="span4"&gt;。显而易见的解决方案是从表单中排除文件上传字段,但这会破坏我的布局。这可能吗?

【问题讨论】:

  • 我不确定这是否有效,但您可以选择带有 JQuey.javascript 的字段并将其值设置为 "" 吗?
  • 在发送表单之前和处理之后,你能不能把那个 的内容替换成一个空字符串?
  • 表单实际上是如何提交的?
  • 在发送表单之前删除输入?
  • @EdoPost:我认为这在这种情况下不起作用,因为您不能修改文件输入元素的“值”属性;它是只读的。

标签: javascript jquery html forms


【解决方案1】:

只需删除name="file" 属性即可。

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 执行以下操作来禁用您的输入字段:

    $('#file').prop('disabled', true);
    

    你可能有这个:

    // 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;
        });
    });
    

    【讨论】:

      【解决方案3】:

      如果您可以在输入中添加“禁用”属性,它将不会与表单一起提交:

      <input type="file" name="file" id="file" class="span4" disabled="disabled">
      

      你可以在你的 js-script 中设置这个属性...

      【讨论】:

        【解决方案4】:

        我需要表单和输入有一个名称标签,所以我要做的就是在我的表单上设置一个方法标签。这样,我的输入可以启用并具有标签名称。

        <form name="form" method="post">
        

        【讨论】:

          猜你喜欢
          • 2017-05-31
          • 2021-08-29
          • 2021-02-25
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          • 2021-12-31
          相关资源
          最近更新 更多