Tôi sẽ giả sử bạn có một số loại điều khiển đầu vào và bạn không muốn nhập phím để tự động gửi khi tai nạn người dùng nhấn phím enter. Nếu vậy bạn có thể đính kèm một sự kiện onkeypress javascript vào mỗi điều khiển mà bạn muốn vô hiệu hóa hành vi này.
function disableEnterKey(e)
{
var key;
if (window.event) key = window.event.keyCode; // Internet Explorer
else key = e.which;
return (key != 13);
}
// In your aspx file Page_Load do the following foreach control you want to disable
// the enter key for:
txtYourTextBox.Attributes.Add("OnKeyPress", "return disableEnterKey(event);");
Nếu bạn cần tắt hoàn toàn biểu mẫu gửi biểu mẫu. trường hợp sử dụng trình xử lý OnKeyDown trên thẻ <body>
trên trang của bạn.
Mã javascript:
if (window.event.keyCode == 13)
{
event.returnValue = false;
event.cancel = true;
}
Với JQuery này sẽ được nhiều bụi, dễ dàng hơn và các phương pháp khuyến khích. Bạn có thể tạo tiện ích mở rộng với:
jQuery.fn.DisableEnterKey =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// return false for the enter key
return (key != 13);
})
})
};
// You can then wire it up by just adding this code for each control:
<script type="text/javascript" language="javascript">
$('#txtYourTextBox').DisableEnterKey();
</script>
tôi kiểm tra tài sản của nút UseSubmitBehaviour nhưng nó có vẻ như nó không giải quyết vấn đề của bạn (ví nút), đúng không? – Canavar
Hãy cẩn thận về chính tả. Tôi nhận thấy bạn đang sử dụng cách viết "hành vi" của người Anh/Canada. Tất nhiên, IntelliSense sẽ bắt được điều đó. :) –
@Canavar, đúng vậy. Tôi đang tìm một tính năng tương tự trong ImageButton. –