javascript - How do I set dropdown option selected by looking at MVC URL -


Thank you for creating such a great resource for learning Hello and Stakeoverflow programmers like me. I am reading a lot of answers here to help with my first MVC project, this is my first time asking.

Here's my dropdown HTML

  & lt; Div class = "dropdown" & gt; & Lt; Id = "select asset" onchange = "location = this.options [this selected index] .value;" & Gt; & Lt; Option value = "/ history / index / all" & gt; All assets & lt; / Options & gt; @ Warak (Various items in model .node info) {& lt; Option value = "/ history / index / @item.node.name" & gt; @ item.node.name & lt; / Options & gt; } & Lt; / Select & gt; & Lt; / Div & gt;   

Once an item is selected from the dropdown, the URL will look like this

Now I try to capture that last part Setting URLs to be selected (G0000106 in this case and selected dropdown option) Here is a javascript that I have done together, but it is not working.

  $ ('# asset select'). ('the option'). (Each function () (function getCurrentPageId) {Var params = window.location.href.split ('/'); var i = params.length; var PageID = Param [i]; return page id;} var current page IG = Meet current page (); var $ this = $ (this); if ($ this.text () == current page id) {$ this.attr ('selected', 'selected'); returned;}} );   

Does this task populate that dropdown list? Is this the best way or is there an HTML assistant that can do it? Thanks

option 1 You have enough of your code:

  function getCurrentPageId () {var params = window.location.href.split ('/'); Return Counsel [params.length - 1]; } Var Page id = getCurrentPageId (); $ ('# Property select'). Search ('selected', true);   

However, your problem was in this line:

  var i = params.length; var page id = param [ii];   

this should be the parameter [i-1] , Because you want to get the last array element.

Option 2. Any simple approach that should work for you location.pathname : < / P>

  $ ('# propertySelect'). Val (window .location.pathname);    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -