![](/img/trans.png)
[英]How do you save data from an html form to a mysql database using javascript?
[英]How do you return data from javascript into a html form?
我想知道是否有人可以提供帮助? 我想做的是从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不太满意,并且不确定如何修改此代码以使其正常工作。 其余的我可以弄清楚,但被困在这里。 谢谢您的帮助,不胜感激。
$('#wordCount').val(wordCount);
$('#totalChars').val(totalChars);
$('#charCount').val(charCount);
$('#charCountNoSpace').val(charCountNoSpace);
使用.val()而不是.html(),因为.val()引用输入字段的值。
表单内的HTML应该包含一个隐藏的输入字段:
<input type="hidden" id="word_count" name="word_count" value="0" />
然后在您的JS中:
$('#word_count').val(wordCount);
全部嵌入到您的函数中:
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);
});
如果表单中有INPUT字段,请使用val()
$( '#的wordCount')。VAL(的wordCount)
这将适用于这样的领域:
请注意,“ id”和“ class”之间是有区别的。 jQuery允许您根据元素的属性选择元素。 就像使用CSS一样,“ id”属性将通过“#”进行选择。 因此,请确保在隐藏字段中定义了“ 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
使用javascript,您可以将value用作输入,也可以将innerHtml用作div或其他基于文本的元素
document.getElementById('field').value = 'asdfsadf';
document.getElementById('field').innerHtml= 'asdfsadf';
另外,也可以使用jquery $ .ajax来代替表单提交(表单提交没有任何问题,但是了解jquery也有很多好处,例如您提出了异步请求
http://api.jquery.com/jquery.ajax/
您将要使用如下所示的隐藏字段并将其显示为表格
<form id="myform" action='posttome.php'>
<input type="hidden" id="wordCount"/>
<input type="submit" value="sbumit"> //Submits Form
</form>
然后使用以下三种方法设置其值:元素html,元素值或javascript变量$('#wordCount')。val()
$('#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
看看这个http://www.hscripts.com/scripts/JavaScript/word-count.php
在线上有很多示例,例如google“ javascript统计文本框中的单词”
一些重要的注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.