我有一个这样的HTML页面:
<html>
<body>
<input type="text" onPaste="return myfunction()"></input>
</body>
</html>
我有这样的JavaScript:
function myfunction(){
var text = ;
if(text == "ABC"){
alert("Accepted");
return true;
}
else{
alert("Rejected");
return false;
}
}
我想将粘贴内容分配String
给var text
这个问题似乎很重复。但是,我想在没有这种情况下实现这一目标jquery
。
这是一个可能的解决方案:http : //jsfiddle.net/Lqdfa9co/
我添加了这个:
onPaste="var e=this; setTimeout(function(){myfunction(e)}, 4);"
并将功能更改为:
function myfunction(e){
var text = e.value;
if(text === "ABC"){
alert("Accepted");
}
else{
alert("Rejected");
}
}
为了获得函数内部的值,必须使用setTimeout
ononPaste
事件。这是由于在分配值之前触发了事件
编辑:为了回答已更改的问题,我更新了“拒绝阶段”,清空了输入值http://jsfiddle.net/Lqdfa9co/1/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句