我将此文本框放在gridview中,该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...
}
}
});
}
}
问题是当我输入内容时,警告消息框仍会弹出。警报不会弹出的唯一时间是文本框显示来自服务器的文本(.i.e。页面加载时)。
有什么理由吗?
此HTML来自浏览器:
<input name="_ctl0:contentMain:cklistIT:gvCKList:_ctl2:txtCompletedBy" type="text" id="contentMain_cklistIT_gvCKList_txtCompletedBy_0" />
除了最后一个字符是0,1,...到5
之外,它们都是相同的答案 0 :(得分:2)
实际文本框的is与jQuery用于搜索的值不匹配。
实际文本框的ID为:
id="contentMain_cklistIT_gvCKList_txtCompletedBy_0"
但是要检查的ID是:
'#contentMain_cklistHR_gvCKList_txtCompletedBy_' + nrow
(chklist部分不同 - 一个是HR,一个是IT)。