gpt4 book ai didi

Javascript onpaste 不工作

转载 作者:行者123 更新时间:2023-12-03 08:23:07 26 4
gpt4 key购买 nike

使用下面的 html 和 Javascript,我计算输入字段中输入的字符数,并将其限制为 30。我还向用户显示一个计数器。

计数器在以下事件上更改其值:onfocusonKeyDownonKeyUponpaste

我无法让 onpaste 工作。当用户使用“CTRL V”粘贴时,计数器会更改值,但使用鼠标/右键单击粘贴时不会更改。我做错了什么?

Jsfiddle: https://jsfiddle.net/ow3ubgp0/

HTML:

<input readonly type="text" name="countdown1" size="5" value="30" />

<input type="text" id="my_text_input" name="my_text_input" onfocus="limitText(this.form.my_text_input,this.form.countdown1,30);" onKeyDown="limitText(this.form.my_text_input,this.form.countdown1,30);" onKeyUp="limitText(this.form.my_text_input,this.form.countdown1,30);" onpaste="limitText(this.form.my_text_input,this.form.countdown1,30);" />

Javascript:

function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}

最佳答案

嗨,请检查以下代码

希望对你有帮助

<html>
<form>
<input readonly type="text" name="countdown1" size="5" value="30" />

<input type="text" id="my_text_input" name="my_text_input" onfocus="limitText(this.form.my_text_input,this.form.countdown1,30);" onKeyDown="limitText(this.form.my_text_input,this.form.countdown1,30);" onKeyUp="limitText(this.form.my_text_input,this.form.countdown1,30);" onpaste="limitText(this.form.my_text_input,this.form.countdown1,30);" onChange="limitText(this.form.my_text_input,this.form.countdown1,30);" oninput="limitText(this.form.my_text_input,this.form.countdown1,30);"/>
</form>
<script>
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
</html>

关于Javascript onpaste 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33665795/

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