jQuery:从选择菜单的文本标签中设置文本字段的值

时间:2015-07-29 03:19:22

标签: javascript jquery

我试图从<select>文本(而不是值)设置文本字段的值,但我无法将其设置为工作

$('.prices_for_' + <%= d.id %>).change(function() {
  $('#order_quantity').text($(this).text())
  $('.price_display').text('$' + $(this).val() + '.00')
});

第二行有效,第一行没有。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

关闭您想要从选择菜单的文本中设置标题的标题 - 您可以使用属性选择器.find option来选择值,然后在其上拨打.text()

$('.prices_for_' + <%= d.id %>).change(function() {
    var text = $(this).find('option[value="' + $(this).val() + '"]').text();
    $('#order_quantity').text(text);
    $('.price_display').text('$' + $(this).val() + '.00')
});
相关问题