开发者

Use Jquery to alter an item in a selectlist?

开发者 https://www.devze.com 2022-12-11 09:49 出处:网络
Before: <select id=\"NumberId\" name=\"NumberId\"> <option value=\"\">ZERO</option> <option value=\"4\">FOUR</option>

Before:

<select id="NumberId" name="NumberId">
  <option value="">ZERO</option>
  <option value="4">FOUR</option>
  <option value="5">FIVE</option>
</select>

Using JQuery, modify the value of the option with an empty value to 0 (zero).

After:

<select id="NumberId" name="NumberId">
  <option value="0">ZERO</option>
  <option value="4">FOUR</option>
  <option value="5">FIVE</option>
</select>开发者_如何学Python

How can I do that?


I think you can do something like:

$("#NumberId option[value='']").val('0');

good luck


Well you can do something like this:

$("#NumberId option[value='']").attr("value", "0");

or

$("#NumberId option[value='']").val("0");

(not 100% sure that works with <option> elements)

But the more obvious thing to do is correctly generate the HTML on the serverside, no?


$("select option[value='']").attr("value", "0");
0

精彩评论

暂无评论...
验证码 换一张
取 消