jQuery Gotcha: val() might select the wrong option at a select element

יום שני, מרץ 22, 2010

Let's imagine you have the following select element: <select id="someSelect">  <option value="">-Select-</option>  <option value="12">21</option>  <option value="11">20</option>  <option value="10">19</option>  <option value="9">18</option>  <option value="8">17</option>  <option value="7">16</option>  <option value="6">15</option>  <option value="5">14</option>  <option value="4">13</option>  <option value="3">12</option>  <option value="2">11</option>  <option value="1">10</option></select> Notice that the text in some options appears as value in other options.you can get a select like this when composing an age drop down for example. Now for the Gotcha: which option will be chosen as a result of the following expression:$("#someSelect").val("12");???Answer: the selected option will be...
תגובה אחת