[英]Can't validate quantity after ajax load content
This is my example: 这是我的示例:
$.ajax({
type: "POST",
url: "some.php",
data: "fname=John&lname=Due",
success: function(result){
$('#content').html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">');
}
});
And js validate quantity 和js验证数量
$(".quantity").keydown(function (e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
// Allow: Ctrl+A, Command+A
(e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
.html()
is a function where parameter that is to be set as new html
is passed at invocation, unless .html(function(index, html){})
pattern is used, where new html
is return
ed at function callback; .html()
是一个函数,在调用时将传递要设置为新html
参数,除非使用了.html(function(index, html){})
模式,否则在函数回调中return
新html
。 not set as a property value as .innerHTML
is. 未设置为.innerHTML
的属性值。 Substitute 替代
$('#content').html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">');
for 对于
$('#content').html = 'Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">';
Use event delegation to attach keydown
event to dynamically created .quantity
elements; 使用事件委托将keydown
事件附加到动态创建的.quantity
元素; or attach event when element is created 或在创建元素时附加事件
function handleKeydown(e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
// Allow: Ctrl+A, Command+A
(e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
$('#content')
.html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">')
.find(".quantity")
.keydown(handleKeydown);
As others has mentioned, you have to use the html() function properly. 正如其他人提到的那样,您必须正确使用html()函数。
By the way, if ever you create html content dynamically, you have to bind the events also. 顺便说一句,如果您动态创建html内容,则还必须绑定事件。
$.ajax({
type: "POST",
url: "some.php",
data: "fname=John&lname=Due",
success: function(result){
$('#content').html = 'Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">';
$(".quantity").unbind("keydown").bind("keydown", function() {
// your validation code should goes here
});
}
});
.html是您无法分配的方法,因此请按照以下步骤进行参考http://api.jquery.com/html/
$('#content').html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.