如何验证 jQuery 中的文本框
How to validate a textbox in jQuery
以下代码在 jQuery 中创建一个 table 和一个文本框。
var table = $("#dynamic-table").children();
var row = $('<tr class="data"></tr>');
row.append($('<td ><input type="text" value=' + item.Marks1 + '
size="10px" class="marks" id="txtmarks1" ></td>'));
我正在尝试使用以下不起作用的代码验证文本框。
$(".data").delegate(".marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
});
如果验证方法不正确,请提出解决方案。
事件目标部分 $(".data")
和目标选择器 ".marks"
都是在页面加载后添加的
所以你 delegate()
方法不会针对部分 .data
选择器 class
您应该使用页面加载时加载的父选择器或 document
作为委托选择器部分
$(document).delegate(".marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
})
$(".marks").keyup(function () {
if ($(this).val().length > 2) {
if ($(this).val() > 100) {
alert("does not exceed 100");
}
}
});
试试这个:
$("body").delegate(".data .marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
})
只需使用:
$(".data").change(".marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
})
试试这个 jsFiddle
$(document).on('keyup', ".marks", function () {
if ($(this).val().length > 2) {
if ($(this).val() > 100) {
alert("does not exceed 100");
}
}
});
试试这个:
$('#dynamic-table').off('keyup').on("keyup",".marks",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
}) ;
以下代码在 jQuery 中创建一个 table 和一个文本框。
var table = $("#dynamic-table").children();
var row = $('<tr class="data"></tr>');
row.append($('<td ><input type="text" value=' + item.Marks1 + '
size="10px" class="marks" id="txtmarks1" ></td>'));
我正在尝试使用以下不起作用的代码验证文本框。
$(".data").delegate(".marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
});
如果验证方法不正确,请提出解决方案。
事件目标部分 $(".data")
和目标选择器 ".marks"
都是在页面加载后添加的
所以你 delegate()
方法不会针对部分 .data
选择器 class
您应该使用页面加载时加载的父选择器或 document
作为委托选择器部分
$(document).delegate(".marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
})
$(".marks").keyup(function () {
if ($(this).val().length > 2) {
if ($(this).val() > 100) {
alert("does not exceed 100");
}
}
});
试试这个:
$("body").delegate(".data .marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
})
只需使用:
$(".data").change(".marks","keyup",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
})
试试这个 jsFiddle
$(document).on('keyup', ".marks", function () {
if ($(this).val().length > 2) {
if ($(this).val() > 100) {
alert("does not exceed 100");
}
}
});
试试这个:
$('#dynamic-table').off('keyup').on("keyup",".marks",function () {
if ($('#txtmarks1').val().length > 2) {
if ($('#txtmarks1').val() > 100) {
alert("does not exceed 100");
}
}
}) ;