简体   繁体   English

在TextBoxFor Enter键按下期间会自动触发按钮单击事件

[英]Button click event being fired automatically during TextBoxFor Enter key press

The issue I am facing now is a button click event is automatically being fired when enter key is pressed in Html.TextBoxFor(). 我现在面临的问题是,在Html.TextBoxFor()中按下Enter键时,会自动触发一个按钮单击事件。 I am having a main view. 我有一个主要观点。 In this view there are 3 buttons. 在此视图中有3个按钮。 On each of 2 button click a partial view is opened and during 3rd button click, a new view is opened. 在两个按钮的每个按钮上单击都会打开一个局部视图,在第三个按钮的单击期间会打开一个新视图。 Please see the code below : 请参见下面的代码:

<script>
    $(document).ready(function () {
        SetUpDatePickers();
    });

    $('#button1').click(function (event) {
        $('#divdefinetraj').toggle();
        $('#button1').hide();
        $('#button2').hide();
        $('#button3').hide();
        event.preventDefault();
        GetTrajectories();
    });

    $('#button2').click(function (event) {
        $('#divRequestTT').toggle();
        $('#button1').hide();
        $('#button2').hide();
        $('#button3').hide();
        event.preventDefault();
    });

    $('#button3').click(function (event) {
        window.location.href = '/UserManagement/UsersList/';
        event.preventDefault();
    });    
</script>

I clicked button1 and the first partial view is opened : 我单击了button1并打开了第一个局部视图:

The partial view has below code : 部分视图具有以下代码:

@Html.TextBoxFor(u => u.TrajName, new { @class = "txtboxclass", @id = "TrajName" })

My issue is when I press "Enter" key inside this text box, the code for button1 in main view is being executed : 我的问题是,当我按下此文本框内的“ Enter”键时,正在执行主视图中的button1的代码:

$('#button1').click(function (event) {
    $('#divdefinetraj').toggle();
    $('#button1').hide();
    $('#button2').hide();
    $('#button3').hide();
    event.preventDefault();
    GetTrajectories();
}); 

This results in all the buttons being hidden and the view becomes useless unless user reloads the view forcefully. 这导致所有按钮都被隐藏,并且除非用户强行重新加载视图,否则视图将变得无用。

I tried to handle the onchange() event of the textboxfor and redirected to below function, but it doesn't handle. 我试图处理textboxfor的onchange()事件,并重定向到下面的函数,但无法处理。

function EnterKeyFilter() {
    if (window.event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
    }
}

Even I tried the same function for div - click() .. It doesn't work. 即使我为div-click()尝试了相同的功能,也无法使用。 When I press the enter key the exact button1 click is being handled with this information event = j…y.Event {originalEvent: MouseEvent, type: "click", timeStamp: 7055.025000000001, jQuery110208686809991100928: true, toElement: button#button1 . 当我按下Enter键时,此信息将立即处理button1的确切event = j…y.Event {originalEvent: MouseEvent, type: "click", timeStamp: 7055.025000000001, jQuery110208686809991100928: true, toElement: button#button1

But I am not clicking it either. 但是我也没有点击它。 The partial view is not part of a form also and form submission is not the issue. 部分视图也不是表单的一部分,表单提交也不是问题。 I am new to ASP.NET MVC and confused with this strange behavior. 我是ASP.NET MVC的新手,并对此奇怪的行为感到困惑。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

If you want to disable your press enter in your keyboard, try this: 如果要禁用键盘上的回车,请尝试以下操作:

$(function () {

//On your document ready function, add this:
      $('html').bind('keypress', function (e) {
          if (e.keyCode == 13) {
               return false;
            }
       });
}

Try to keep all three buttons in different form tags and made them submit button. 尝试将所有三个按钮保留在不同的表单标签中,并使它们提交按钮。 So in this case, whenever you hit enter in a form input, respective button will be clicked. 因此,在这种情况下,每当您在表单输入中按Enter键时,都会单击相应的按钮。 To prevent full page postback, use e.preventDefault() in button click event. 为防止整个页面回发,请在按钮单击事件中使用e.preventDefault()。

HTML: HTML:

<form id="frm1">
  -------------
  -------------
 <input id="btnSubmit" type="submit" value="submit" />
</form>

jQuery jQuery的

$("#btnSubmit").click(function(e){
    e.preventDefault();
    -- rest of code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM