简体   繁体   中英

JQuery Page Refreshes

I working on a page with some JQuery and Kendo UI. This is my first JQuery project and I getting things along. However, my page refreshes for some reason. This is what I am trying to do: I have a text field where I can enter a search term and when I press a button, the query is sent to a php file and some json info will pop up. So far, I can get it to return something, but the page refreshs and all the data is gone.

code:

*<!DOCTYPE html>
<html>
<head>
    <title>Search</title>
    <link href="styles/kendo.common.min.css" rel="stylesheet" />
    <link href="styles/kendo.default.min.css" rel="stylesheet" />
    <script src="js/jquery.min.js"></script>
    <script src="js/kendo.web.min.js"></script>
</head>
<body>
<div id="example">
    <form id="search">
        <label for="search">Search For:</label>
        <input type="text" id="txtSearch" name="q">
        <button type="submit" id="submit">Find</button>
    </form>
    <div id="grid">
    </div>
</div>    
    <script>
        $(function() {
            $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: "include/showsearch.php"
                    },
                    schema: {
                        data: "data"
                    }
                },
                columns: [{field: "id"},{field: "name"},{field: "season"}]
            });

            $("#submit").click(function(){
                var textVal = $("#txtSearch").val();
                var dynamicURL = "include/showsearch.php?show_name=" + textVal; 
                var grid = $("#grid").data("kendoGrid");
                alert("sdf123");
                grid.dataSource.transport.options.read.url = dynamicURL;
                grid.dataSource.read();
                alert("sdf");
            });         
        });
    </script>

</body>
</html>*

NOTE: I used the alert functions to stop and see how the page reacts. How do I get the page from refreshing?

The reason this is happening is that the default action for your submit button is still occurring; submitting the form.

It's probably best to catch the form submission event rather than the button click as hitting Enter in a text field may also submit the form.

You will also need to prevent the default event action.

Change this

$("#submit").click(function(){

to this

$('#search').on('submit', function(e) {
    e.preventDefault();
    // and the rest of your code here

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