简体   繁体   English

我该如何提交 <textarea> 我的&lt;form&gt;之外的哪个?

[英]How can I submit a <textarea> which is outside my <form>?

My website is separated into two parts - a large (CodeMirror) <textarea> and a drop-down menu which has a <form> (that contains "From", "To", "Subject" inputs) in it linked to a send.php file. 我的网站分为两个部分-一个大的(CodeMirror) <textarea>和一个下拉菜单,其中包含一个<form> (包含“发件人”,“收件人”,“主题”输入),并链接到send.php文件。

The text area and the form itself are located inside different <div> s so I'm not able to link it to the rest of the inputs I'm transferring to the send.php file. 文本区域和表单本身位于不同的<div>因此我无法将其链接到要传输到send.php文件的其余输入。

How can I link / connect the submit button to the <textarea> input along with the other inputs it's associated with ("From", "To", "Subject") when transferring the data to the send.php file? 将数据传输到send.php文件时,如何将提交按钮与<textarea>输入以及与之关联的其他输入(“发件人”,“收件人”,“主题”)链接/连接起来?

<div id="container">

   <div id="sidebar" class="twenty"> //the form div

      <li class="menu">
          <li class="dropdown">     
             <form method="post" action="send.php">      
                <input type=... />
                <input type=... />
                <input type=... />
                <input type="image" src=... alt="Submit Form" />                
             </form>
          </li>
      </li>

   </div>

   <div class="seventy"> //the textarea div

   <textarea id="code" name="code">
   </textarea>

   </div>

</div>

You can make form the outermost tag. 您可以使表格成为最外面的标签。 It changes nothing to the flow of the document. 它对文档流没有任何改变。

Technically, you could use the form attribute to associate a textarea with a form: 从技术上讲,您可以使用form属性将textarea与表单相关联:

<form ... id="foobar">
...
</form>
...
<textarea form="foobar" ...>

This is an HTML5 feature supported by Chrome and Firefox, for example, but not IE 9. 例如,这是Chrome和Firefox支持的HTML5功能,但IE 9不支持。

So check out other options, primarily reorganizing the page as suggested by @niomaster or using JavaScript as suggested by @Fluffeh. 因此,请查看其他选项,主要是按照@niomaster的建议重新组织页面,或者按照@Fluffeh的建议使用JavaScript。 However, it's not a good idea to rely on JavaScript being enabled, in matters of basic functionality. 但是,就基本功能而言,依靠JavaScript启用并不是一个好主意。 In reorganizing the page, care should be taken to avoid messing up any styling (or scripting) that might rely on the current markup. 在重新组织页面时,应注意避免弄乱可能依赖当前标记的任何样式(或脚本)。 Also note that the current markup is invalid, since li elements are allowed only as children of ol or ul , so restructuring (if feasible) would be recommendable anyway. 还要注意,当前标记无效,因为li元素仅允许作为olul子元素,因此无论如何建议进行重组(如果可行)。

At the simplest, it might suffice to move the <form ...> start tag to the very start of the body element and the </form> end tag right before the end of the body element. 简单地说,将<form ...>开始标签移动到body元素的最开始,并将</form>结束标签移动到body元素的末尾可能就足够了。 Then all form field elements on the page would be fields of this form. 然后,页面上的所有表单字段元素都将是此表单的字段。

You can't directly - if it's not in a form, it can't be submitted. 您不能直接-如果它不是表单,则无法提交。

The best you can do is to write a custom javascript function that replaces your 'submit (image type)' action and copies the data from the textform into a hidden field within the form then submits the form as the last action. 最好的办法是编写一个自定义javascript函数,该函数将替换“提交(图像类型)”操作并将数据从文本表单复制到表单中的隐藏字段 ,然后将表单提交为最后一个操作。 This will get the results you want without the user really knowing what you are doing behind the scenes. 这将获得您想要的结果,而用户无需真正知道您在幕后所做的事情。

Edit: As niomaster correctly points out, forms can span more than just a single <div> or <li> attribute. 编辑:正如niomaster正确指出的那样,表单可以跨越的范围不仅限于单个<div><li>属性。 You can extend it easily without changing your code structure. 您可以轻松扩展它,而无需更改代码结构。

use jquery 使用jQuery

//Get the text from text area //从文本区域获取文本

var textareadata = $("#code").val();

//dynamically append a hidden feild to your form //动态地将隐藏的字段添加到表单中

$('form').append('<input type="hidden" name="myfieldname" value="myvalue" id='myvalue'/>');

// dynamically write your text adtea data to the hidden field append to the form //动态将您的文本adtea数据写入到附加到表单的隐藏字段中

$('#myvalue').val(textareadata);

amiregelz, 阿米雷格兹,

You can just add one id to form tag. 您可以仅向表单标签添加一个ID。 using jquery get the value of the text area [$("#text-areaID").val();]and add hidden field using jquery/ javascript, pass the value to hidden field which you created dynamically then after validation of the form submit the form. 使用jquery获取文本区域[$(“#text-areaID”)。val();]的值,并使用jquery / javascript添加隐藏字段,将该值传递给您动态创建的隐藏字段,然后在验证表单后提交表格。 You can get the value of the textarea as well in your php page. 您也可以在php页面中获得textarea的值。

Hope It will help you. 希望对您有帮助。 Please mark it answer if it helps. 如果有帮助,请标记为答案。

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

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