SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question form submit with 2 selected values

    Hello,

    I need to submit a form with a url query like this ?car=black

    What would be the jquery code?

    Thanks

    Code:
    <select id="opt1">
    
       
        <option selected="selected">red</option>
        <option>black</option>
        
      </select>
      <select id="opt2">
    
        <option>car</option>
        <option selected="selected">bus</option>
        <option>train</option>
        
      </select>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You don't need any JavaScript to accomplish this, the following would be sufficient to do the above.

    HTML Code:
    <form action="" method="get">
        <select name="color">
            <option value="red">Red</option>
            <option value="black">Black</option>
        </select>
        
        <select name="type">
            <option value="car">Car</option>
            <option value="bus">Bus</option>
            <option value="train">Train</option>
        </select>
        
        <input type="submit" value="Submit" />
    </form>

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    You don't need any JavaScript to accomplish this, the following would be sufficient to do the above.

    HTML Code:
    <form action="" method="get">
        <select name="color">
            <option value="red">Red</option>
            <option value="black">Black</option>
        </select>
        
        <select name="type">
            <option value="car">Car</option>
            <option value="bus">Bus</option>
            <option value="train">Train</option>
        </select>
        
        <input type="submit" value="Submit" />
    </form>

    Hello, Thanks Chris. But query in your code is ?color=black&type=car because of your added select name attributes.


    Actually I am working on WordPress custom post type where I need the exact query like this ?car=black or ?<2nd selected option>=<1st selected option> to retrieve data.

    Thats why I need javascript/jquery.

  4. #4
    SitePoint Zealot bgil's Avatar
    Join Date
    Apr 2009
    Location
    So. California
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your form has a submit button with an id="submit"...

    Code JavaScript:
    $("#submit").click(function () { $("#opt1").attr("name", $("#opt2").attr("value")); });
    -Brad

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgil View Post
    If your form has a submit button with an id="submit"...

    Code JavaScript:
    $("#submit").click(function () { $("#opt1").attr("name", $("#opt2").attr("value")); });
    Thanks bgil, I tried with attr before your post but could not make it but you did!.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •