Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

html - Alpine JS - modal won't close unless @click is duplicated

I'm using Alpine JS. I have a field which when clicked displays a modal. When I click away from the modal it closes. When an <li> within the modal is clicked the modal also closes.

                    <div class="relative inline-block text-left">
                    
                
                    <div 
                        x-data="{ journeyModal: false, journeyType: 'Oneway' }"
                        @click="journeyModal = !journeyModal" 
                        class="cursor-pointer">
                        <div class="flex">
                            <input disabled x-bind:value="journeyType" id="journeyType" type="text" name="journeyType" autocomplete="off" class="hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-indigo-500 inline-flex justify-center w-32 px-4 py-2 text-sm font-medium text-gray-700 bg-white border-0 rounded-md cursor-pointer" />

                        </div>

                            <div x-show.transition.duration.200ms="journeyModal" @click.away="journeyModal = false" class="ring-1 ring-black ring-opacity-5 absolute left-0 z-10 w-56 mt-1 origin-top-right bg-white rounded-md shadow-lg">
                                <div @click=" journeyModal = !journeyModal" class="py-1" role="menu" aria-orientation="vertical" aria-labelledby="options-menu">
                                    <li @click=" journeyModal = !journeyModal, journeyType = 'Roundrtip' " class="hover:bg-gray-100 hover:text-gray-900 block px-4 py-2 text-sm text-gray-700" role="menuitem">Rountrip</li>
                                    <li @click=" journeyModal = !journeyModal, journeyType = 'Oneway' " class="hover:bg-gray-100 hover:text-gray-900 block px-4 py-2 text-sm text-gray-700" role="menuitem">Oneway</li>
                                    <li @click=" journeyModal = !journeyModal, journeyType = 'Multicity' " class="hover:bg-gray-100 hover:text-gray-900 block px-4 py-2 text-sm text-gray-700" role="menuitem">Multicity</li>  
                                </div>
                                
                            </div>
                            <p class="pl-96" x-text='journeyModal' ></p>
                    </div>
                </div>

This is working as required. However, I found that it will only work if the DIV containing the <li> tags also includes the Alpline JS - @click=" journeyModal = !journeyModal", i.e. repetition of the same directive as exists within the li tags.

I don't understand why this is needed to toggle the value of journeyModal.

question from:https://stackoverflow.com/questions/65927769/alpine-js-modal-wont-close-unless-click-is-duplicated

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...