1

I have this simple html page:

<html>
    <head>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
         $(document).on('click', 'button', function(){
             var selectval = $('select').val();
             $.get('/',
             {
                  selectval: selectval
             });
         });
    });
    </script>
    </head>

    <body>
    <select>
         <option value="Option1">Option1</option>
         <option value="Option2">Option2</option>
    </select>

    <button>Submit</button>
    </body>
</html>

I want the button to act like a submit button on a form with get method. So clicking the button should submit the get variable (selectval) as a query string.

So clicking the button should go to:

http://domain.com/?selectval=option1

Is there a way to do this without wrapping into a form?

3
  • you should ids to the elements Commented Sep 27, 2014 at 13:52
  • Why don't you want to wrap into a form? Commented Sep 27, 2014 at 13:52
  • i don't like using forms much. Also interested to know if this is doable at all or not Commented Sep 27, 2014 at 13:55

2 Answers 2

3

Try this as javascript:

<script type="text/javascript">
    $(document).ready(function () {
         $(document).on('click', 'button', function(){
             var selectval = $('select').val();
             location.href = "?selectval="+selectval;
         });
    });
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

:) Sometimes solutions are simpler than our expectations. Why should we try harder way? :)
1

you can do it and it is perfectly valid to do so. You can try the following method to do it. Note I have used IDs for the required elements.

<html>
    <head>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
         $('#btn').click( function(){
             var selectval = $('#opt').val();
             $.get('/',
             {
                  selectval: selectval
             },function(data){
                 alert(data);
             });
         });
    });
    </script>
    </head>

    <body>
    <select id="opt">
         <option value="Option1">Option1</option>
         <option value="Option2">Option2</option>
    </select>

    <button id="btn">Submit</button>
    </body>
</html>

Fiddle Here

2 Comments

sorry tried this and it did nothing...it didn't submit anything
It is working for me, the only thing i changed is use jquery from different source. src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js" http://code.jquery.com/jquery-latest.min.js doesn't seem to load from my machine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.