我有一个文本框。我根据单选按钮用于两个不同的条目。当选择单选按钮时,我更改name属性并删除旧类并添加新类(keyup-numeric或keyup-charNumOnly)。根据课程的不同,执行编辑功能。使用Chrome DEV工具,我看到课程正在按预期更改(回到第四个没问题)。但是keyup-numeric函数是唯一被执行的函数。我是JQUERY的新手。所以这可能是不经意的。 感谢帮助。
$(document).ready(function() {
$("#radio1").click(function() {
$('p.error-keyup-1').remove();
$("#requestedInput").val("Update Address using Account Number").attr('name','acctNumber').attr('maxlength','10');
$("#requestedInput").removeClass().addClass('inputNumTextBox keyup-numeric');
});
$("#radio2").click(function() {
$('p.error-keyup-1').remove();
$("#requestedInput").val("Update Account Number using Address").attr('name', 'Address').attr('maxlength','23');
$("#requestedInput").removeClass().addClass('inputNumTextBox keyup-charNumOnly');
} );
$('.keyup-numeric').keyup(function() {
$('p.error-keyup-1').remove();
var inputVal = $(this).val();
var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
var AcctReg9 = /^9/;
if(!numericReg.test(inputVal)) {
$('<p class="error-keyup-1 errorStyle centerText">Numeric characters only</p>').appendTo('#enterRequestDataSection');
}
if(!AcctReg9.test(inputVal)) {
$('<p class="error-keyup-1 errorStyle centerText">Accounts Start with 9 .... Required</p>').appendTo('#enterRequestDataSection');
}
if ($('#requestedInput').val().length==0) {
$('p.error-keyup-1').remove();
}
});
$('.keyup-charNumOnly').keyup(function() {
alert('keyup-charNumOnly I');
$('p.error-keyup-1').remove();
var inputVal = $(this).val();
var characterReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
if(!characterReg.test(inputVal)) {
alert('keyup-charNumOnly II');
$('<p class="error-keyup-1 errorStyle centerText">cRAP</p>').appendTo('#enterRequestDataSection');
}
if ($('#requestedInput').val().length==0) {
$('p.error-keyup-1').remove();
}
});
}); // End of JQUERY CODE
<div id="requestActionTypeRow">
<ul class="requestActionType">
<li class="ax_text_field"><input type="radio" id="radio1" name="radio" value="radioAcctInp" checked="checked" >Account Number</li>
<li class="ax_text_field"><input type="radio" id="radio2" name="radio" value="radioSwiftInp" >Show alphaNumeric Address</li>
</ul>
</div>
<div id="enterRequestDataSection">
<ul class="enterRequestDataType">
<li><input type="text" id="requestedInput" name="acctNumber" value="Update Address using Account Number" maxlength="10" class="inputNumTextBox keyup-numeric"></li>
<li id="showDetails" name="showDetails" value="Show Details" onclick="validateAcctMaint(this.form)">Show Details</li>
<li class="addNewLink">Add New</li>
</ul>
</div>
答案 0 :(得分:0)
当keyup事件在文档之后绑定时,keyUpCharNumOnly类不存在(ready(。
您将要使用:
$('.keyup-charNumOnly').on('keyup', function() {
答案 1 :(得分:0)
试试这个:
$(document).ready(function() {
$("#radio1").click(function() {
$('p.error-keyup-1').remove();
$("#requestedInput").val("Update Address using Account Number").attr('name','acctNumber').attr('maxlength','10');
$("#requestedInput").removeClass().addClass('inputNumTextBox keyup-numeric');
});
$("#radio2").click(function() {
$('p.error-keyup-1').remove();
$("#requestedInput").val("Update Account Number using Address").attr('name', 'Address').attr('maxlength','23');
$("#requestedInput").removeClass().addClass('inputNumTextBox keyup-charNumOnly');
} );
$(document).on('keyup', "#requestedInput.keyup-numeric", function() {
$('p.error-keyup-1').remove();
var inputVal = $(this).val();
var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
var AcctReg9 = /^9/;
if(!numericReg.test(inputVal)) {
$('<p class="error-keyup-1 errorStyle centerText">Numeric characters only</p>').appendTo('#enterRequestDataSection');
}
if(!AcctReg9.test(inputVal)) {
$('<p class="error-keyup-1 errorStyle centerText">Accounts Start with 9 .... Required</p>').appendTo('#enterRequestDataSection');
}
if ($('#requestedInput').val().length==0) {
$('p.error-keyup-1').remove();
}
});
$(document).on('keyup', "#requestedInput.keyup-charNumOnly", function() {
alert('keyup-charNumOnly I');
$('p.error-keyup-1').remove();
var inputVal = $(this).val();
var characterReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
if(!characterReg.test(inputVal)) {
alert('keyup-charNumOnly II');
$('<p class="error-keyup-1 errorStyle centerText">cRAP</p>').appendTo('#enterRequestDataSection');
}
if ($('#requestedInput').val().length==0) {
$('p.error-keyup-1').remove();
}
});
}); // End of JQUERY CODE