Tarik Setia Tarik Setia - 6 months ago 69
Javascript Question

Form Submit not working jquery

When ever I click submit button, page just get refreshed. However this code works everywhere.
The form id is #pager. Code that is note working as expected is




<!DOCTYPE html>
<html lang="en">
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.css">


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.js"></script>
// The Edit and
function actionFormatter(value, row, index) {
return [
'<a class="edit ml10" href="javascript:void(0)" title="Edit">',
'<i class="glyphicon glyphicon-edit"></i>',
'<a class="remove ml10" href="javascript:void(0)" title="Remove">',
'<i class="glyphicon glyphicon-remove"></i>',
].join(' ');

crossDomain: true,
beforeSend: function (request)
request.setRequestHeader("Content-Type", "application/json");
url: "http://localhost:5000/api/members/",
processData: false,
success: function(msg) {
data: msg

$(function () {

$('#memberTable').on('all.bs.table', function (e, name, args) {
.on('check.bs.table', function (e, row) {


window.actionEvents = {

'click .edit': function (e, value, row, index) {
'click .remove': function (e, value, row, index) {


<div class="container">
<div class="row">
<div class="col-sm-12">
<h2>Member Data</h2>
<table id="memberTable" data-search="true"
<th data-field="state" data-checkbox="true"></th>
<th data-field="name" data-sortable="true" >Name</th>
<th data-field="phone">Phone</th>
<th data-field="date" data-sortable="true">Date</th>
<th data-field="action" data-formatter="actionFormatter" data-events="actionEvents">Action</th>
<div class="row">
<div class="col-sm-12">
<form id = "pager">

<textarea class="form-control" rows="3" id="textArea"></textarea>
<span class="help-block"></span>
<button type="submit" id="sendButton" class="form-control btn btn-primary">Send</button>





Looks like there are two problems here. The first is that you need to prevent the default form behavior so that the page doesn't redirect.

The second is that you need to instantiate your listener after the document is ready. At the moment your listener gets added before the DOM is ready so it's never actually running. This should solve both problems.

$(function() {
  $("#pager").submit(function(e) {

  // You should probably put the rest of your Javascript in here too, so it doesn't run until the DOM is fully ready.

You can read more about event.preventDefault here. And document.ready here.