简体   繁体   中英

Get an option value from a URL to present a specific menu item from a drop down list

I've got this great jQuery tablesorter going, but Im missing a key part. Details: The table is made of hundreds of report links categorized by topic. They topics are dividable by a simple select drop down menu.

What Im looking for is the ability to get an option value from a URL to present a specific menu item from a drop down list. That way I can have a URL that shows only one topic of the rows (ex: 30 of 500 reports).

I know php would be easy, but that's not an option, and I think some use of document.location.search could get me there.

Of course Im a javascript rookie so does anyone see how to implement this code-wise?

Thanks folks, much appreciated!

 <script type="text/javascript">
$(function() {
            $("#example-4").addClass("beautifulData").beautify(
                    {
                        pageSize : 25,
                        pagerSize : 5                           
                    }); 

    $("#txt_topics").keyup(function() {
        $("#example-4").beautify("rebuild", { filter :
                    { 6 :$("#txt_topics").val() } });
    });     

    $("#select1").change(function() {
             $("#example-4").beautify("rebuild", { filter : 
                    { 6 :$("#select1").val() } });
    });

 <select id="select1"> 
 <option value="">Choose a Topic</option>

 <option value="topic_A">Topic A</option> 
 <option value="topic_B">Topic B</option>
 <option value="topic_c">Topic C</option>     

The window.location object contains the URL information. You can use this to pull out the query parameters that you are interested. Chrome has a window.location.search property that contains the '?' and everything after. Should get you where you want to go. Example for url "http://www.myawesomesight.com/tabledata?option=topicA":

var queryString = window.location.search.substr(1); //substr dumps the leading '?'
var queryParams = queryString.split('&');
for (var i in queryParams) {
  var tokens = queryParams[i].split('=');
  var key = tokens[0];
  var value = tokens[1];

  // insert logic here to handle when you find the key that you want
}

There is probably a better way of doing it, but this is the method that I have used on my current project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM