簡體   English   中英

為什么我的 html 代碼中的 function 沒有被調用?

[英]Why my function in html code is not being called?

我正在開發一個使用 TailwindCSS 和 AlpineJS 的 Laravel Livewire 項目。 但我的問題是關於我的 setCountry() function 沒有被調用。 我可以看到參數被正確傳遞,但 function 本身沒有被調用。 toggleCountriesHandler() function 運行良好。 我敢打賭這是我眼皮底下的東西,但我仍然無法弄清楚,而且我是編程的初學者。 這是我的代碼片段:

 toggleCountriesHandler = () => { let toggleCountries = document.getElementById("countriesOpen"); if (toggleCountries.style.display === "none") { toggleCountries.style.display = "block"; } else { toggleCountries.style.display = "none"; } } setCountry = (country_id, country_code, country_name) => { document.getElementById("country-id").value = country_id; document.getElementById("selected-country").innerHTML = "<img class='w-5 h-5 rounded-full' src='img/flags/16/" + country_code + ".png'>" + "<span class='block ml-3 font-normal truncate'>" + country_name + "</span>"; document.getElementById("countriesOpen").style.display = "none"; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.2.0/tailwind.min.css" rel="stylesheet" /> <span class="block text-sm font-medium text-gray-700 transform translate-y-5">{{__("Country")}</span> <div style="height: 42px;" class="relative flex items-center justify-center w-full pl-3.5 pr-10 text-left bg-white border border-gray-300 rounded-md shadow-sm cursor-default focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"> <button onclick="toggleCountriesHandler()" class="block w-full truncate focus:outline-none"> <label id="selected-country" class="flex flex-row items-center text-gray-500">{{__("Select your country")}}</label> </button> <div class="absolute top-0 right-0 w-full mt-12 bg-white rounded-md shadow-lg sm:bg-white"> <input id="country-id" type="hidden" name="country_id"> <ul id="countriesOpen" style="display: none;" class="py-1 overflow-auto text-base rounded-md sm:border max-h-60 ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm"> @foreach(DB::table('countries')->get() as $country) <li role="option" class="relative "> <button onclick="setCountry({{ $country->id }}, '{{ $country->code }}', '{{ $country->name }}')" type="button" class="flex items-center w-full py-2 pl-3 space-x-3 cursor-default select-none text-extrading-text-primary sm:text-gray-900 pr-9 focus:outline-none hover:bg-indigo-600 hover:text-white"> <img class="w-5 h-5 rounded-full" src="{{ asset('img/flags/16/'. $country->code. '.png') }}"> <span class="block font-normal truncate"> {{$country->name}} </span> </button> </li> @endforeach </ul> </div> </div>

我發布我的評論作為結束這個問題的答案。

const toggletoggleCountriesHandler=()=>{}

const通常用於 function 聲明,因為人們不會更改它。 你仍然可以使用varlet也用於聲明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM