Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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/2/jquery/80.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 动态添加时onKeydown不起作用_Javascript_Jquery_Html_Twitter Bootstrap - Fatal编程技术网

Javascript 动态添加时onKeydown不起作用

Javascript 动态添加时onKeydown不起作用,javascript,jquery,html,twitter-bootstrap,Javascript,Jquery,Html,Twitter Bootstrap,我试图使用jQuery在我的网页中添加一些html。我的代码是 var s1 = "<div class='controls'><label class='inline labelspace'>Minimum Cart Value<input class='inline input-small' type='text' name='mincart' onKeydown=isNumberKey ></input></label>"; s

我试图使用jQuery在我的网页中添加一些html。我的代码是

 var s1 = "<div class='controls'><label class='inline labelspace'>Minimum Cart Value<input class='inline input-small' type='text' name='mincart' onKeydown=isNumberKey ></input></label>";
s1 += "<label class='inline labelspace'>Minimum No Of Items<input class='inline input-small' type='text' placeholder='Enter Integer' name='minitem' onKeydown=isNumberKey></input></label>" ;
s1 += "<label class='inline labelspace'>Location<input class='inline input-small' type='text' name='location' ></input></label>";
s1 += "<label class='inline labelspace'>Product Ids<input class='inline input-small' onKeydown=isValid type='text' name='products'></input></label></div>" ;
$("#lastrow").before(s1);
var s1=“最低购物车价值”;
s1+=“最小项目数”;
s1+=“位置”;
s1+=“产品ID”;
元(“#最后一行”)。在(s1)之前;
isNumberKey是一个源代码为

function isNumberKey(evt) {
  console.log('coming to this');
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    } else 
        return true;

}
函数isNumberKey(evt){
console.log('coming to this');
var charCode=(evt.which)?evt.which:event.keyCode;
如果(charCode!=46&&charCode>31&&charCode<48|charCode>57)){
返回false;
}否则
返回true;
}
使用这些功能,只能在文本框中输入数字。当我指定这样的元素时
$(“#一些元素”).onKeyDown(isNumberKey)
那么它工作正常,但我想知道为什么它在第一种情况下不工作。

您必须
委派事件。使用:

$(document).on('keydown' , '.some-class', isNumberKey);
如果
元素添加到标记中,则
事件将不会绑定到它们上。所以你需要授权

使用
class
而不是
id
。当然,
需要添加到
附加的
div
中,或者在这种情况下,添加到
输入中


如果在('event',function(){})
上使用
$('.some class')。则事件将仅附加到具有
.some class
的现有元素上。使用委派将在您想要的任何
div
上附加
事件。

如果您想在HTML中附带sh,请尝试更改下面的代码

onKeydown="isNumberKey()"

$(“#某些元素”).onKeyDown(isNumberKey)
$(document)之间的区别。on('keydown','some#elem',isNumberKey)
是第一个绑定到现有对象,第二个将处理每个选定对象,即使它是在绑定后创建的。告诉如何在HTML属性中完成它。如果需要,您必须在dom ready上将其运行到脚本页。@steo抱歉,只是想弄清楚为什么它会工作。那是在你的编辑之前,我不知道为什么我得了负分。他的问题
$(“#某些元素”).onKeyDown(isNumberKey)
那么它工作正常,但我想知道为什么在第一种情况下它不工作
objeelement.onKeyDown=isNumberKey
我认为根据javascript语法这是正确的。@U Green先生,但更正代码中的上述行会工作正常吗?根据问题,@user1671639似乎是对的。想知道为什么jQuery中没有名为
onKeyDown
的事件。我认为您正在寻找
按键下压事件。如果您想了解这应该如何工作,请查看我答案中的演示。