1

I'm pretty new to jQuery and I've been given a task I really have no idea how to complete. Here's the situation:

I have a form as follows:

Code:

<form action="search.php" method="get" id="cse-search-box">
  <input type="text" name="q" size="31"/>
  <select name="category">
    <option>One</option>
    <option>Two</option>
    <option>Three</option>
  </select>
  <select name="contributors">
    <option>One</option>
    <option>Two</option>
    <option>Three</option>
  </select>
  <input type="submit" name="sa" value="Search" />
</form>

The form is used to submit a query to google site search. The input field 'q' is the main search query.

I need to combine the user's choices in the 'category' and 'contributors' fields into one variable, named 'as_q', and insert this into a form field on submission.

Thanks in advance for any help

2
  • Can you include the working code you have so far so we can see the specifics of what you are trying to accomplish? Commented Sep 18, 2009 at 15:02
  • I don't really have any working code! The Google site search code uses the variables q and as_q to build a search query. If I only had one select field, it'd be called as_q; and there would be no problem. But as I'm using two select fields, they need to be combined to make one string. The Google site search code is then included in the search.php file, and retrieves the search terms via GET. Not sure if it makes sense! So I want to combine the two select fields into one variable so that it can be used as a search string. Commented Sep 18, 2009 at 16:00

2 Answers 2

1
<script>
    $('#cse-search-box').bind('submit', function(){
        var category = $('[name=category]').val();
        var contributors = $('[name=contributors]').val();
        $('[name=q]').val(category+' '+contributors);
    });
</script>

Have this piece of script after the <form> is declared (it's html, i'm not very sure if it is appropriate to use declare)

Sign up to request clarification or add additional context in comments.

Comments

0

The best way to do this would be to add a hidden field with the name you need - as_q - and concatenate whatever fields you'd like into this field on the form submit.

So you'd add this to your form:

<input id="as_q" name="as_q" type="hidden">

And then populate it like so:

$('#cse-search-box').submit(function() {
  $('#as_q').val($("[name='category']").val() + $("[name='contributors']").val());
});

Comments

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.