user3503616
user3503616

Reputation: 5

How to separate two forms in a page

I have two forms in a page but when i click on one button the other form runs too and >>>two windows of the same page<<< will open. How can i separate the function of these two or more forms in a page?

The code is as below. another form has the same code with different web addresses.

<form method="post">
             <input id="element 1_1" class="element radio1" name="element 1" type="radio" value="http://www.yahoo.com" /> 
    MODEL 13<br/>
    <br/>
    <input id="element 2_1" class="element radio1" name="element 1" type="radio" value="http://www.msn.com" />
    MODEL 17<br/><br/>
    <input id="element 3_1" class="element radio1" name="element 1" type="radio"   value="http://www.ebay.com" /> 
    MODEL 22 <br/><br/>

<script>
$(function () {
    $("form").submit(function (e) {
        e.preventDefault();
        var url = $(this).find('input[type="radio"]:checked').val();
        window.open(url);

    });
});
</script></form>

other coder

<form method="post">
              <input id="element 1_2" class="element radio" name="element 2" type="radio" value="http://www.123.com" /> 
    MODEL 5 - 8<br/>
    <br/>
    <input id="element 2_2" class="element radio" name="element 2" type="radio" value="http://www.543.com" />
    MODEL 6-10<br/>
    <br/>
<script>
$(function () {
    $("form").submit(function (e) {
        e.preventDefault();
        var url = $(this).find('input[type="radio"]:checked').val();
        window.open(url);

    });
});
</script></form>

Upvotes: 0

Views: 1239

Answers (1)

bprayudha
bprayudha

Reputation: 1034

You can use attribute id to each form.

<form method="post" id="form1">
   ...
</form>

<form method="post" id="form2">
   ...
</form>

<script>
$(function () {
  $("#form1").submit(function(e) {
    // function for form1
  });
  $("#form2").submit(function(e) {
    // function for form2
  });
});
</script>

Upvotes: 1

Related Questions