-2

I am trying to make an Asp.Net Mvc Ajax project.I have to take the value of select-option in a loop with jquery.All select's ids are same , names are same when I try it takes the first select's value.btw I have tried all things in this title: jQuery Get Selected Option From Dropdown. Can you help me please?

@foreach (var item in Model)
{
    <select id="selSaat">
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
}
Community
  • 1
  • 1
Seuphoria
  • 17
  • 7
  • 2
    Show the code you have tried and the html. And what do you mean _"All select's ids are same"_ -? That's invalid html. –  Jun 23 '15 at 00:12
  • its valid in asp.net mvc – Seuphoria Jun 23 '15 at 00:18
  • It is not. And it has nothing to do with MVC. Its invalid html! –  Jun 23 '15 at 00:20
  • 1
    Use a class name instead of an `id` attribute, but why on earth are you creating a manual ` –  Jun 23 '15 at 00:22
  • If you want a simple jQuery solution it is below, and it is very simple. If you wish to use the framework like it is supposed to be used, you would use the script line I just wrote along with the approach that Mr. Muecke is suggesting. Your doing an app like a webpage, dont do that. Use your data model to populate your views select by running it through a loop containing all of your select items, labels and values. If all that is to much for you, stick to static HTML until you master the DOM and CSSOM, jQuery and JS. – AlphaG33k Jun 23 '15 at 00:43

1 Answers1

0

Heres some JS code that will give you the selections value if there is only 1 select element on your page and the options within it contain values on them:

document.getElementsByTagName('select')[0].onchange = function(){
    alert(this.value);
};

Heres a working demo for ya:

http://codepen.io/nicholasabrams/pen/OVOPYK

Targeting a <select> with native browser javascript with the onchange method will return the value selected when it occurs via user interaction.

http://www.w3schools.com/jsref/event_onchange.asp

AlphaG33k
  • 1,588
  • 1
  • 12
  • 24