我想限制在文本框中使用逗号。最初,文本框已禁用,只有当用户单击相关复选框时,才会启用文本框。
以下是我尝试的代码:
function checkForComma(keyCode) {
if (event.keyCode == 188) {
alert('Not allowed');
}
}
编辑模板:
@Html.TextBoxFor(m => m.AddressLine1, @Model.isUsed ?
(object)new { @class = "form-control" } :
new { @class = "form-control", @disabled = "disabled", @onkeypress = "checkForComma(this);" })
@Html.ValidationMessageFor(m => m.AddressLine1)
答案 0 :(得分:2)
您忘了在函数中放回false
。修改你的功能
function checkForComma(event) {
if (event.charCode == 44) {
alert('Not allowed');
return false;
}else{
return true;
}
}
同时修改您的html代码如下
@Html.TextBoxFor(m => m.AddressLine1, @Model.isUsed ?
(object)new { @class = "form-control" } :
new { @class = "form-control", @disabled = "disabled", @onkeypress = "return checkForComma(event);" })