admin管理员组文章数量:1426385
So Im working with alpinejs and I want to make a "select" where the options e from an alpinejs x-data that returns an array of nodes. I know how to do it in vanilla js where you just need to create a loop and then append those elements to the parent element. But have no clue of how to do it in alpine since Ive tried different options and none are working.
<div x-data="{ options: $el.querySelectorAll('.m-tab__title') }">
<select class="w-full md:hidden">
<template x-for="option in options">
<option x-html="option"></option>
</template>
</select>...
That just doesnt display any content in the select but [object HTMLDivElement].
<div x-data="{ options: $el.querySelectorAll('.m-tab__title') }">
<select class="w-full md:hidden">
<template x-for="option in options">
<option x-text="option.innerText"></option>
</template>
</select>...
While that displays just the inner Text of each option but not even inside a proper select.
Any way I can display each option as a select option?
Thanks!
So Im working with alpinejs and I want to make a "select" where the options e from an alpinejs x-data that returns an array of nodes. I know how to do it in vanilla js where you just need to create a loop and then append those elements to the parent element. But have no clue of how to do it in alpine since Ive tried different options and none are working.
<div x-data="{ options: $el.querySelectorAll('.m-tab__title') }">
<select class="w-full md:hidden">
<template x-for="option in options">
<option x-html="option"></option>
</template>
</select>...
That just doesnt display any content in the select but [object HTMLDivElement].
<div x-data="{ options: $el.querySelectorAll('.m-tab__title') }">
<select class="w-full md:hidden">
<template x-for="option in options">
<option x-text="option.innerText"></option>
</template>
</select>...
While that displays just the inner Text of each option but not even inside a proper select.
Any way I can display each option as a select option?
Thanks!
Share Improve this question edited Jun 8, 2021 at 5:53 valiano 18.7k7 gold badges71 silver badges83 bronze badges asked Jun 5, 2021 at 10:10 Ger Ger 211 gold badge1 silver badge2 bronze badges1 Answer
Reset to default 1Are you sure your options array contain valid data?
If unsure you could always add x-init="console.log(options);"
to your "x-data div"
I made a quick working example here: https://codepen.io/outoforbit/pen/JjWaJKN
<div x-data="{ selectedOption: 'option1',
options: [
'option1',
'option2',
'option3'
]}">
<div class="w-80 flex pr-3">
<p class="w-1/2">Select option:</p>
<div class="w-1/2 inline-block relative">
<select name="my_option" x-model="selectedOption" class="">
<template x-for="option in options" :key="option">
<option :value="option" x-text="option"></option>
</template>
</select>
</div>
</div>
Select option: 本文标签: javascriptHow to display select options on alpinejsStack Overflow
版权声明:本文标题:javascript - How to display select options on alpinejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745464040a2659452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论