从动态选择选项中获取文本(Get text from dynamic select option)

编程入门 行业动态 更新时间:2024-10-06 18:31:54
从动态选择选项中获取文本(Get text from dynamic select option)

我有多个选择选项,我想在.texts上写文字car或motorcycle

这是一些动态选择选项

<td> <select id="area-0"> <option value="1">Car</option> <option value="2">Motorcycle</option> </select> </td> <td class="texts"></td> <td> <select id="area-1"> <option value="1">Car</option> <option value="2">Motorcycle</option> </select> </td> <td class="texts"></td>

这是我的剧本

$('select[id^=area]').change(function(){ var text_t = $(this option:selected).text(); $(this).parent().next().html(text_t); });

我怎么能把car或motorcycle放在.texts

I have more than one select option, i want to write text car or motorcycle on .texts

This a few dynamic select option

<td> <select id="area-0"> <option value="1">Car</option> <option value="2">Motorcycle</option> </select> </td> <td class="texts"></td> <td> <select id="area-1"> <option value="1">Car</option> <option value="2">Motorcycle</option> </select> </td> <td class="texts"></td>

this my script

$('select[id^=area]').change(function(){ var text_t = $(this option:selected).text(); $(this).parent().next().html(text_t); });

How can i place car or motorcycle on .texts

最满意答案

您可以使用.find()方法获取所选option的文本。 根据当前实现,您必须获得语法错误。

var text_t = $(this).find('option:selected').text();

代替

var text_t = $(this option:selected).text();

You can use .find() method to get the selected option's text. As per current implementation you must be getting syntax error.

var text_t = $(this).find('option:selected').text();

instead of

var text_t = $(this option:selected).text();

更多推荐

本文发布于:2023-08-07 12:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   动态   选项中   text   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!