客户端代码:
<script type="text/javascript">
var counter = 0;
function increaseCounter(textAID, textBID)
{
var textA = document.getElementById(textAID);
var textB = document.getElementById(textBID);
if (isNullOrWhitespace(textA.value) && isNullOrWhitespace(textB.value))
counter++;
}
function isNullOrWhitespace( input ) {
if (input == null) return true;
return input.replace(/\s/g, '').length < 1;
}
</script>
isNullOrWhitespace 的代码取自https://stackoverflow.com/a/5559461/1495902。
服务器端代码:
for (int i = 0; i < TextboxA.Count() ; i++)
{
var script = "increaseCounter(" + TextboxA[i].ClientID + "," + TextboxB[i].ClientID + ")";
TextboxA[i].Attributes.Add("onchange", script);
TextboxB[i].Attributes.Add("onchange", script);
}