简体   繁体   English

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

[英]jQuery: set value of text field from text label of select menu

I'm trying to set the value of a text field from the text (not the value) of a <select> , and I can't get it to work. 我正在尝试从<select>文本 (而不是值)设置文本字段的值,但无法正常工作。

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

The second line works, the first doesn't. 第二行有效,第一行无效。 Any ideas? 有任何想法吗?

Going off the title where you said you want to set it from the text of the select menu - you could use an attribute selector to .find the option with the value that was selected, and then call .text() on it: 从您要从选择菜单的文本中进行设置的标题开始,-您可以使用属性选择器.findoption并带有选定的值,然后在其上调用.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')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM