我有这个文本框位于用户控件中的 gridview 内。我正在尝试使用下面的 jQuery 代码检查文本框是否为空。
$(document).ready(function () {//when Dom is ready to be used
$('input[type=submit]').click(function () {
CheckIfCheckBoxIsChecked(this); //Use the clicked button to proceed
});
});
function CheckIfCheckBoxIsChecked(e) {
var $id = $(e).prop('id'); //get the id of the clicked button
var $gvid = "";
//Set the name of the gridview
if ($id.indexOf('IT') != -1) {
$gvid = "contentMain_cklistIT_gvCKList";
}
else if ($id.indexOf('HR') != -1) {
$gvid = "contentMain_cklistHR_gvCKList";
}
else if ($id.indexOf('Marketing') != -1) {
$gvid = "contentMain_cklistMarketing_gvCKList";
}
else if ($id.indexOf('Payroll') != -1) {
$gvid = "contentMain_cklistPayroll_gvCKList";
}
else if ($id.indexOf('Property') != -1) {
$gvid = "contentMain_cklistProperty_gvCKList";
}
var $AllHM = $('#' + $gvid + ' input[id*="ckHMreq"]:checkbox:checked'); //select only checked checkboxes that have ckHMreq as part of their name
var n = $AllHM.length;
if (n == 0) {//If there's none, telle the user
alert('Please check at least one row to proceed');
}
else {//If there's at least one
$.each($AllHM, function () {//Loop through each of them
cid = $(this).prop('id');
var nrow = cid.match('\\d+'); //find the row number
var ckDpt = $('#' + $gvid + ' input[id*="ckDpt_' + nrow + '"]:checkbox'); //select the appropriate checkbox
if ($(ckDpt).prop('checked') == true) {// if the checkbox is selected
var a = $('#contentMain_cklistHR_gvCKList_txtCompletedBy_' + nrow);
if (a.val() == "") {
alert('Please enter your name to proceed');
//more to come...
}
}
});
}
}
问题是当我输入一些东西时,仍然会弹出警告消息框。唯一不弹出警报的时间是文本框显示来自服务器的文本时(即页面加载时)。
有什么理由吗?
编辑
此 HTML 来自浏览器:
<input name="_ctl0:contentMain:cklistIT:gvCKList:_ctl2:txtCompletedBy" type="text" id="contentMain_cklistIT_gvCKList_txtCompletedBy_0" />
除了最后一个字符是 0、1、.. 到 5 之外,它们都是相同的