0

I'm working on a project which includes some JQuery and AJAX. I have also implemented a feature of JQuery autofill. The code of form is shown below:

<form class="form-horizontal">
    <div class="form-group">
        <label for="polciynumber" class="col-sm-2 control-label">Please Enter Policy Number:</label>
        <div class="col-sm-4 ui-widget">
            <input id="endtpolnumber" type="text" class="form-control col-sm-1" name="endtpolnumber" placeholder="Plase Enter Policy Number">
            <button id="findpolinfo" class="btn btn-primary col-sm-3">Find Policy</button>
        </div>
        <label for="insuredsname" class="col-sm-2 control-label">Department:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="endtdept" name="endtdept" placeholder="" disabled="disabled">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
            <button class="btn btn-primary" id="addendt">Add Endorsement</button>
            <button class="btn btn-warning" id="addendtclearform">Clear Form</button>
        </div>
    </div>
</form>

The Autofill is implemented on the textbox with ID "endtpolnumber". When I click on textbox with ID "findpolinfo" it should alert "asdf". But, on contrary it is redirecting me to to this URL

"http://localhost/dis/home/login?endtpolnumber=31-125&endtefffrom=&endtno=&endtamt=&endtdesc="

My JQuery code is as below:

$('#findpolinfo').click(function () {
        alert('asdf');
    });

Does anyone have any idea, where am i going wrong?

All positive suggestions are welcomed...

Thanks in advance...

V15HM4Y
  • 1,757
  • 4
  • 26
  • 40

3 Answers3

0

Because the default behavoiur of <button> if it is inside form is submit

what's the standard behavior when < button > tag click? will it submit the form?

add

type="button"

if you don't want this button to submit form

Luke
  • 362
  • 1
  • 4
  • 13
0

Button has a default behaviour to submit the form. You can use return false to stop that form submission. Below is code to help you out. Just enter the policy number and click on the 'Find Policy' button:

$('#findpolinfo').click(function () {
        alert($("#endtpolnumber").val());
        return false;
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal">
    <div class="form-group">
        <label for="polciynumber" class="col-sm-2 control-label">Please Enter Policy Number:</label>
        <div class="col-sm-4 ui-widget">
            <input id="endtpolnumber" type="text" class="form-control col-sm-1" name="endtpolnumber" placeholder="Plase Enter Policy Number">
            <button id="findpolinfo" class="btn btn-primary col-sm-3">Find Policy</button>
        </div>
        <label for="insuredsname" class="col-sm-2 control-label">Department:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="endtdept" name="endtdept" placeholder="" disabled="disabled">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
            <button class="btn btn-primary" id="addendt">Add Endorsement</button>
            <button class="btn btn-warning" id="addendtclearform">Clear Form</button>
        </div>
    </div>
</form>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
0

based on W3schools if you want to add button just for clicking,You can add type="button" to your button attributes.just like this:

<form class="form-horizontal">
  <div class="form-group">
    <label for="polciynumber" class="col-sm-2 control-label">Please Enter Policy Number:</label>
    <div class="col-sm-4 ui-widget">
      <input id="endtpolnumber" type="text" class="form-control col-sm-1" name="endtpolnumber" placeholder="Plase Enter Policy Number">
      <button type="button" id="findpolinfo" class="btn btn-primary col-sm-3">Find Policy</button>
    </div>
    <label for="insuredsname" class="col-sm-2 control-label">Department:</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="endtdept" name="endtdept" placeholder="" disabled="disabled">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-3 col-sm-9">
      <button type="button" class="btn btn-primary" id="addendt">Add Endorsement</button>
      <button type="button" class="btn btn-warning" id="addendtclearform">Clear Form</button>
    </div>
  </div>
</form>
Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35