gpt4 book ai didi

javascript - 表单输入的字符计数器 - 不计数

转载 作者:行者123 更新时间:2023-12-03 11:17:31 25 4
gpt4 key购买 nike

有人知道为什么这段代码不显示剩余字符而只显示最大字符数,我已经检查了好几次了,我似乎找不到错误。

HTML

<form class="comment" method="post" action="postComment.php">
<input type="text" placeholder="Name" name="Cname" onKeyUp="charLeft(this.value,30,'n')"><br/><span id="nCharLeft"></span><br/>
<input type="text" placeholder="Email" name="Cemail" onKeyUp="charLeft(this.value,50,'e')"><br/><span id="eCharLeft"></span><br/>
<textarea rows="4" placeholder="Please leave a comment." name="Ccomment" onKeyUp="charLeft(this.value,300,'c')"></textarea><br/><span id="cCharLeft"></span><br>
<input type="submit" value="Post Comment"><br/>
</form>

JavaScript

function charLeft(val,len,indi) {
var output = indi + "CharLeft";
if (val.length==0) {
return;
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById(output).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","charLeft.php?q="+val+"&m="+len,true);
xmlhttp.send();
}

PHP(charLeft.php)

<?php
$v = $_REQUEST['v'];
$m = $_REQUEST['m'];
$len = strlen($v);
$charleft = $m - $len;

echo $charleft==="1" ? "$charleft character left." : "$charleft characters left.";
?>

最佳答案

在您的 AJAX 调用中,您在 q 中发送值,但将其访问为$v = $_REQUEST['v'];

因此,要么将 AJAX 调用更改为xmlhttp.open("GET","charLeft.php?v="+val+"&m="+len,true);

或将 PHP 更改为$v = $_REQUEST['q'];

关于javascript - 表单输入的字符计数器 - 不计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27270856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com