Search code examples
jqueryajaxjqgrid

How to Stop MVC Page Refresh After Ajax Call url Change when Keyboard Enter Pressed


When i Write text in Text box and Press Enter then My page refresh and URL Change i am Upload Page Snap and You can see in Image . Here is my JQgrid code when i enter text in textbox then data show in jQgrid .

How to Stop page refresh when i enter textbox value and press keyboard Enter button .

Here is my code of HTML AND Jquery i want to stop page refresh when enter value in text box and Press Enter then my page Regresh and page link has change

Here is Page link http://localhost:11736/Home/Index?

jQuery(document).ready(function($) {

  jQuery("#searchAll").change(function() {
    var $grid = $("#searchGrid");

    $grid.jqGrid({
      url: '@Url.Action("Search_All")',
      datatype: 'json',
      postData: {
        mSearch: function() {
          return $("#searchAll").val();
        }
      },
      jsonReader: {
        id: 'MemberShipID'
      },
      colModel: [{
          name: 'MemberShipID',
          index: 'MemberShipID',
          label: 'MemberShip ID',
          width: 20
        },
        {
          name: 'MembershipName',
          index: 'MembershipName',
          label: 'Membership Name',
          width: 30
        },
        {
          name: 'address',
          index: 'address',
          label: 'address',
          width: 50
        },
        {
          name: 'address1',
          index: 'address1',
          label: 'address1',
          width: 50
        },
        {
          name: 'FileID',
          index: 'FileID',
          label: 'File ID',
          width: 20
        },
        {
          name: 'SectorName',
          index: 'SectorName',
          label: 'SectorName',
          width: 20
        },
        {
          name: 'PhaseName',
          index: 'PhaseName',
          label: 'PhaseName',
          width: 20
        },
        {
          name: 'PlotCategory',
          index: 'PlotCategory',
          label: 'PlotCategory',
          width: 20
        },
        {
          name: 'RPersonName',
          index: 'RPersonName',
          label: 'RPersonName',
          width: 20
        },
        {
          name: 'CityName',
          index: 'CityName',
          label: 'City',
          width: 10
        },
        {
          name: 'CountryName',
          index: 'CountryName',
          label: 'Country',
          width: 10
        }
      ],
      additionalProperties: [],
      loadonce: true,
      navOptions: {
        reloadGridOptions: {
          fromServer: true
        }
      },

      formEditing: {

        closeOnEscape: true,
        closeAfterEdit: true,
        savekey: [true, 13],
        reloadGridOptions: {

          fromServer: true
        }
      },
      viewrecords: true,
      height: 300,
      width: 1200,
      rowNum: 100,
      autoheight: true,
      rowList: [10, 20, 30, 50, 100, 500],
      pager: "#jqGridPager",
      rownumbers: true

    });

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="app-search">
  <input type="text" class="form-control" id="searchAll" placeholder="Search for..."> <a class="srh-btn"><i class="ti-search"></i></a>
</form>


Solution

  • As far as I understand, your problem is the default submit behavior of form tag that raise submit event when Enter key is pressed.

    To prevent that event you can use the following code

    jQuery(document).ready(function(){
        jQuery("form.app-search").submit(function(e){
            e.preventDefault();
        });
    })
    

    Where jQuery("form.app-search") is a jquery selector that look for form tags having class app-search, even you can write a very specific Id(# selector).

    Try FIDDLE

    Hope this works for you.