在IE中输入文本字段中按下输入键时按钮OnClick触发

时间:2022-07-07 19:43:25

I have a couple of input textboxes on my webpage for searching:

我的网页上有几个输入文本框用于搜索:

<input type="text" id="mfrText"  name="MfrSearchText" value='@ViewBag.SearchAndSort.MfrSearchText' />
<input type="text" id="partText"  name="PartNoSearchText" value="@ViewBag.SearchAndSort.PartNoSearchText" />  </
<input type="text" id="descText" name="DescriptionSearchText" value="@ViewBag.SearchAndSort.DescriptionSearchText" /> 

I have a button that has a click event to show a dialog box.

我有一个按钮,有一个单击事件来显示一个对话框。

<button class="btnAdd btn-xs">Add</button>

The issue I have is when the enter key is hit and one of the input fields has the focus, the button onclick event is fired and the dialog box is displayed. I have tried e.preventDefault() and I have also tried checking if the enter key was pressed instead of a mouse click with no luck. This happens in IE but not in Chrome. How do I prevent this behavior ?

我遇到的问题是当按下回车键并且其中一个输入字段具有焦点时,将触发按钮onclick事件并显示对话框。我已经尝试了e.preventDefault(),我也尝试检查是否按下了回车键而不是鼠标点击没有运气。这在IE中发生,但在Chrome中没有。我该如何防止这种行为?

My click event:

我的点击事件:

$(".btnAdd").click(function (e) {

         alert($(".btnAdd").is(":focus"));
         var partID = $(this).closest('tr').attr('id');
         $("#divAdd").dialog({
             modal: true,
             title: 'Increase Qty',
             buttons: {
                 "Save": function () {
                     var qty = $("#addQtyAdd").val();
                     var clockNo = $("#addClockNo").val();
                     var asset = $("#addAssetNo").val();

                     AddPart(partID, qty, clockNo, asset);
                 },
                 Cancel: function () {
                     $(this).dialog("close");
                 }
             }
         });
         $(".ui-dialog-title").css("color", "lightgreen");
     });

1 个解决方案

#1


5  

That is desired/intended behaviour.

这是期望/预期的行为。

Try:

<button type="button" class="btnAdd btn-xs">Add</button>

This will stop the button from being seen as a submit control, and will be ignored by Enter keypresses.

这将使该按钮不被视为提交控件,并且Enter键将被忽略。

#1


5  

That is desired/intended behaviour.

这是期望/预期的行为。

Try:

<button type="button" class="btnAdd btn-xs">Add</button>

This will stop the button from being seen as a submit control, and will be ignored by Enter keypresses.

这将使该按钮不被视为提交控件,并且Enter键将被忽略。