我创建了两个文本框,并限制用户不要在母版页中使用keydown事件来允许文本。
<label>Amount</label>
<div>
<asp:TextBox ID="txtAmount" class="form-control" runat="server"></asp:TextBox>
</div>
<label>Amount</label>
<div>
<asp:TextBox ID="txtAmountvalue" class="form-control" runat="server"></asp:TextBox>
</div>
if ("input[id$='txtAmount']")
$(this).keydown(function (event) {
if ((event.keyCode >= 48 && event.keyCode <= 57) ||
(event.keyCode >= 96 && event.keyCode <= 105) ||
(event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 12) ||
(event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 9) ||
(event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) ||
(event.keyCode == 110)) {
return true;
} else {
return false;
}
});
if ("input[id$='txtAmountvalue']")
$(this).keydown(function (event) {
if ((event.keyCode >= 48 && event.keyCode <= 57) ||
(event.keyCode >= 96 && event.keyCode <= 105) ||
(event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 12) ||
(event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 9) ||
(event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) ||
(event.keyCode == 110)) {
return true;
} else {
return false;
}
});
我创建了两个keydown事件,但两个文本框的给定值相同。所以我的问题是如何在一个函数中组合keydown事件?
答案 0 :(得分:0)
你可以简单地使用jquery包含选择器(id * =&#39; yourid&#39;),因为你知道他们的身份证明在他们的ID中txtAmount
文字
如beow片段:
$("input[id*='txtAmount']").keydown(function(event) {
if ((event.keyCode >= 48 && event.keyCode <= 57) ||
(event.keyCode >= 96 && event.keyCode <= 105) ||
(event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 12) ||
(event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 9) ||
(event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) ||
(event.keyCode == 110)) {
console.log("true");
return true;
} else {
console.log("false");
return false;
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Amount</label>
<div>
<input id="txtAmount" class="form-control" runat="server" />
</div>
<label>Amount</label>
<div>
<input id="txtAmountvalue" class="form-control" runat="server" />
</div>
&#13;