5

Please find below my code:

Template of customer search form

<script type="text/x-kendoui-template" id="customer-search-view-template">
    <div class="searchform" id="searchCustomer">
        <form class="frmSearch">
            <input name="searchTxt" data-bind="value: customerName" class="k-textbox" />
            <button class="k-button" data-bind="click: searchClicked">Search</button>
            <button class="k-button" data-bind="click: newClicked">New</button>
        </form>             
    </div>
</script>

customer-search.js where loading above template and creating viewmodel object

$(function(){

    var views = {};
    templateLoader.loadExtTemplate("customer-search-view-template", "../views/customer-search-template.html");

    var layout = new kendo.Layout($('#customer-search-view-template').html());
    layout.render($("#main"));

    // Create an observable view model object.
    var customer = kendo.observable({
        customerName: "John",

        searchClicked: function() {
            this.set("customerName", "Search clicked");         
        },

        newClicked: function() {
            this.set("customerName", "New clicked");            
        }

    });

    // Bind the view model to the personFields element.
    kendo.bind($('#searchCustomer'), customer);

});

When I click the search button, the text is set in the textbox but this also refresh the page with ?searchTxt=Search+clicked in the address bar.

May I know why this button click refresh the page and how do I stop refreshing the page on button click ???

Valay
  • 1,991
  • 2
  • 43
  • 98

2 Answers2

6

I would try and place the attribute 'type' for each like so:

<button type="button" class="k-button" data-bind="click: searchClicked">Search</button>
<button type="button" class="k-button" data-bind="click: newClicked">New</button>

The page thinks that each are performing a form submit action, but by placing the type attribute, you can access the event you intended for search. You may not need your form tags if you are not going to post any data, but rather just a js event handler. Good luck.

kryptonkal
  • 874
  • 8
  • 23
  • I tried with `type=button` but still it refreshes the page-@kryptonkal – Valay Jun 05 '14 at 18:15
  • 1
    I still suspect that it is performing a post on the page. Try and override the button event and include this: e.preventDefault(); e.stopImmediatePropagation(); console.log('here') or alert('here') choose one. – kryptonkal Jun 05 '14 at 18:51
  • `e.preventDefault(); e.stopImmediatePropagation();` works. Thanks.- @kryptonkal – Valay Jun 06 '14 at 11:15
0

The reason is that you are inside a <form>, which has no settings (URL, method, etc), so the browser's default behavior is probably to perform a GET to the current URL (which is a refresh). You could just use <div> instead of <form> if you just want to execute that method.