ほんじゃらねっと

ダイエット中プログラマのブログ

jQueryでselectメニューで選択中のoptionの表示テキストを取得する

これまたもっといい方法があると思われるが、一応メモしておく。

<script type="text/javascript" src="/static/javascripts/jquery-1.1.3.1.pack.js"></script>
<script type="text/javascript">
  $(function() {
      $("#add_customer_btn").click(function() {
        if ($("#customer_select").val() != "") {
          var customer_id = $("#customer_select").val();
          alert($("#customer_select/option[@value=" + customer_id + "]").text());
        }
      });
  });
</script>
<div style="margin: 3px 0 3px 0;">
<select id="customer_select">
<option value="">選択して下さい</option>
{% for customer in customer_list %}
<option value="{{ customer.id }}">{{ customer.name }}</option>
{% endfor %}
</select>
<input type="button" id="add_customer_btn" value="追加" />
</div>


Vimにもスーパーはてな記法にも「htmldjango」なんていうシンタックスがあった。
いやー、何でも用意されてるもんだ。