Input 从“提交文本”;div contenteditable";

Input 从“提交文本”;div contenteditable";,input,submit,contenteditable,insertonsubmit,Input,Submit,Contenteditable,Insertonsubmit,我正试图找到一个有效的解决办法,但目前没有任何有用的办法。我有以下表格: <form id="searchform" action="/search" onsubmit="putText()"> <div class="section" > <div class="edit"><div ContentEditable = "true" id="text"></div></div> <div class="control

我正试图找到一个有效的解决办法,但目前没有任何有用的办法。我有以下表格:

<form id="searchform" action="/search" onsubmit="putText()">
<div class="section" >
<div class="edit"><div ContentEditable = "true" id="text"></div></div>
<div class="control" onclick="sbmt()"></div>
</div>
<input id="s" name="q" class="submit" type="submit" value="Send">
</form> 
(我也使用jquery-1.8.3.js) 查询应类似于“mysite.com/search?q=”。 我只需要使用div class=“control”作为提交按钮,而不是简单的输入(只需将输入按钮替换为div)。当我按下输入按钮时,一切正常,但当我使用DIV class=“control”提交时不起作用-表单提交,但没有查询文本。我不能只使用输入,因为submit div是第三方api的一部分


谢谢您的建议。

您正在尝试对非表单元素使用表单操作。请改用输入元素。

如果要获取#text div中的数据,可以使用jQuery从div中获取数据:

function sbmt() {
  var param = '';

  // jQuery
  param = $('#text').text();

  // or you could use pure javascript
  param = document.getElementById('text').innerHTML;

  // Now that you have these values you should add them to your form as an input
  // input fields are how you send data through forms to wherever you are posting them
  // build the input field
  param = "<input type='text' name'param1' value='" + param + "'/>";

  // append it to the form
  $('#searchform').append(param);

  // finally submit the form.
  document.getElementById("searchform").submit();
}
函数sbmt(){
var参数=“”;
//jQuery
param=$('#text').text();
//或者可以使用纯javascript
param=document.getElementById('text').innerHTML;
//既然有了这些值,就应该将它们作为输入添加到表单中
//输入字段是通过表单将数据发送到发布位置的方式
//构建输入字段
param=“”;
//将其附加到表单中
$(“#searchform”).append(param);
//最后提交表格。
document.getElementById(“searchform”).submit();
}
然后,您将能够根据在输入字段中分配的
名称
访问提交的参数(在本例中为param1

如果你想锻炼一下正在发生的事情,可以尝试一下这个方法。

contentediate.coffee example.html


来源:

但是表单是由div提交的,我只需要从“contenteditable”字段中用?q=”获取一个文本。这没关系,表单不是为了发送表单元素标记中嵌套的所有信息,而是为了发送控件中指定的值,div不是受支持的控件类型。他当然有理由问,也许他在DIV中做了一个像我这样的富文本编辑器,或者其他什么,需要一个非AJAX提交的后备方案。因此,我建议在提交之前将DIV的内容复制到每个JS的输入中,一切正常。您是否尝试在#text DIV中提交文本?还有,你想把它提交到哪里?是的,我想提交#text div内容。我需要提交它作为一个搜索查询。Thanx!人们告诉我这是不可能的),但我相信它能起到一些作用
function sbmt() {
  var param = '';

  // jQuery
  param = $('#text').text();

  // or you could use pure javascript
  param = document.getElementById('text').innerHTML;

  // Now that you have these values you should add them to your form as an input
  // input fields are how you send data through forms to wherever you are posting them
  // build the input field
  param = "<input type='text' name'param1' value='" + param + "'/>";

  // append it to the form
  $('#searchform').append(param);

  // finally submit the form.
  document.getElementById("searchform").submit();
}
$('[contenteditable_form]').on 'submit', (e) -> 
  $form = $ e.target
  $form.find("[data-hidden-input]").each (i, e) ->
    $contenteditable = $ e
    attr = $contenteditable.data('hiddenInput')

    hidden_input = $form.find "[#{attr}]"

    value = $contenteditable.html()

    $(hidden_input).val value
<input class="hidden" hidden_description name="some_name" type="hidden"> 
<div contenteditable="true" data-hidden-input="hidden_description"></div>