简体   繁体   中英

Button OnClick Fires When Enter Key Is Pressed In Input Text Field In IE

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 ?

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");
     });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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