Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将数据从javascript返回到html表单中?_Javascript_Jquery_Html - Fatal编程技术网

如何将数据从javascript返回到html表单中?

如何将数据从javascript返回到html表单中?,javascript,jquery,html,Javascript,Jquery,Html,我想知道是否有人能帮忙?我试图做的是从javascript代码中检索字数到一个表单中,然后将其与表单的其余部分一起传递到php,该表单将检查字数是否为某个长度,否则不会提交 javascript如下所示 counter = function() { var value = $('#msg').val(); if (value.length == 0) { $('#wordCount').html(0); $('#tot

我想知道是否有人能帮忙?我试图做的是从javascript代码中检索字数到一个表单中,然后将其与表单的其余部分一起传递到php,该表单将检查字数是否为某个长度,否则不会提交

javascript如下所示

    counter = function() {
       var value = $('#msg').val();

       if (value.length == 0) {
          $('#wordCount').html(0);
          $('#totalChars').html(0);
          $('#charCount').html(0);
          $('#charCountNoSpace').html(0);
          return;
       }

       var regex = /\s+/gi;
       var wordCount = value.trim().replace(regex, ' ').split(' ').length;
       var totalChars = value.length;
       var charCount = value.trim().length;
       var charCountNoSpace = value.replace(regex, '').length;

       $('#wordCount').html(wordCount);
       $('#totalChars').html(totalChars);
       $('#charCount').html(charCount);
       $('#charCountNoSpace').html(charCountNoSpace);
   };

   $(document).ready(function() {
      $('#count').click(counter);
      $('#msg').change(counter);
      $('#msg').keydown(counter);
      $('#msg').keypress(counter);
      $('#msg').keyup(counter);
      $('#msg').blur(counter);
      $('#msg').focus(counter);
   });

我的问题是将wordCount返回到表单中的隐藏字段中。我对javascript不是很在行,也不知道如何修改这段代码使其正常工作。剩下的我能搞清楚,但我被困在这里。感谢您的帮助,非常感谢。

表单中的HTML应包含一个隐藏的输入字段:

然后在JS中:

$‘字数’。valwordCount

所有这些都嵌入到您的函数中:

    counter = function() {
       var value = $('#msg').val();

       if (value.length == 0) {
          $('#wordCount').html(0);
          $('#totalChars').html(0);
          $('#charCount').html(0);
          $('#charCountNoSpace').html(0);
          return;
       }

       var regex = /\s+/gi;
       var wordCount = value.trim().replace(regex, ' ').split(' ').length;
       var totalChars = value.length;
       var charCount = value.trim().length;
       var charCountNoSpace = value.replace(regex, '').length;

       $('#wordCount').html(wordCount);
       $('#word_count').val(wordCount);
       $('#totalChars').html(totalChars);
       $('#charCount').html(charCount);
       $('#charCountNoSpace').html(charCountNoSpace);
   };

   $(document).ready(function() {
      $('#count').click(counter);
      $('#msg').change(counter);
      $('#msg').keydown(counter);
      $('#msg').keypress(counter);
      $('#msg').keyup(counter);
      $('#msg').blur(counter);
      $('#msg').focus(counter);
   });

使用.val而不是.html,因为.val指的是输入字段的值。

如果表单中有输入字段,请使用val

$'wordCount'.valwordCount

这对于这样的领域是可行的:


请注意,id和类之间存在差异。jQuery允许您根据元素的属性选择元素。id属性被选中,就像在CSS中一样。因此,请确保在隐藏字段中定义了id='wordCount'。

显示的代码不仅是javascript,还包括jquery,请确保包含jquery

<script src = "http://code.jquery.com/jquery-1.11.1.min.js"></script>
$('#field').val('asdf'); //Sets Value of a input type="text"
$('#field').html('sadf'); //Sets the html of a div

也不使用表单提交考虑使用jQuery $ .Ajax,表单提交没有什么错误,但对了解jQuery也有好处,比如您来做异步请求< /P> 您将希望使用一个隐藏字段,如以下所示,并将其保存在表单中

<form id="myform" action='posttome.php'>
    <input type="hidden" id="wordCount"/>
    <input type="submit" value="sbumit"> //Submits Form
</form>
看看这个

网上有很多例子,只有谷歌javascript在文本框中计算单词

一些重要的注意事项:

没有空格的很长字符串仍然是一个单词,因此不要忘记设置字段的最大长度 如果您这样做是作为一种验证,请注意这样一个事实:您不能信任表单字段,因为表单字段很容易被操作,因此不要忘记在提交表单后检查服务器端的字数。
您可能有一个html部分在表单中的某个地方?不确定问题出在哪里。。。所以你想隐藏字数?只需对css隐藏它。否则我就不明白了,对不起:这是用户的显示字段吗?您可以使用隐藏的输入字段,只需使用$wordCountValue.valwordCount;
document.getElementById('field').value = 'asdfsadf';
document.getElementById('field').innerHtml= 'asdfsadf';
<form id="myform" action='posttome.php'>
    <input type="hidden" id="wordCount"/>
    <input type="submit" value="sbumit"> //Submits Form
</form>
$('#wordCount').val($('#wordCountSoruceDiv').html()); // Sets the value to another divs html
$('#wordCount').val($('#wordCountSourceInput').val()); // Sets the value to another inputs value
$('#wordCount').val(wordCountVariable); // Sets the value to a variable