0

我正在使用 geocompelete js 库从 google 获取位置,但是当我从下拉列表中更改国家/地区时,它会显示以前选择的国家/地区结果,所以当我选择另一个国家/地区时我想重新初始化 detailsAttribute 这是我的 js 代码。我还在这里添加了我的原始 html,您可以查看并提供最佳解决方案。谢谢

$(document).ready(function(){
    $("#user_country_id").change(function() {
      value =$("#user_country_id").find(':selected').attr('alpha_code');
      $("#user_address").geocomplete({
        componentRestrictions: {country: value},
        details: "form",
        detailsAttribute: "user-data-geo"
      })
    });
  });


<form class="simple_form form" id="new_user" novalidate="novalidate" enctype="multipart/form-data" action="/admin/users" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="oEnMXiVNKoFizscwtSaRzEyGSQjBaBKdoU9oldGVASbGJUsCbNVJ/jZ8kIUs5110y4KSy/OhEWSYEEN0lfgMKg=="><div class="input string required user_first_name"><label class="string required" for="user_first_name"><abbr title="required">*</abbr> First name</label><input class="string required" required="required" aria-required="true" type="text" name="user[first_name]" id="user_first_name"></div>
<div class="input string required user_last_name"><label class="string required" for="user_last_name"><abbr title="required">*</abbr> Last name</label><input class="string required" required="required" aria-required="true" type="text" name="user[last_name]" id="user_last_name"></div>
<div class="input email required user_email"><label class="email required" for="user_email"><abbr title="required">*</abbr> Email</label><input class="string email required" required="required" aria-required="true" type="email" value="" name="user[email]" id="user_email"></div>
<div class="input hidden user_role field_without_errors"><input value="Trainer" class="hidden" type="hidden" name="user[role]" id="user_role"></div>
<div class="input boolean required user_is_active field_without_errors"><input value="0" type="hidden" name="user[is_active]"><label class="boolean required checkbox" for="user_is_active"><input class="boolean required" required="required" aria-required="true" type="checkbox" value="1" checked="checked" name="user[is_active]" id="user_is_active"><abbr title="required">*</abbr> Is active</label></div>
<div class="input integer optional user_country_id"><label class="integer optional" for="user_country_id">Country</label><select name="user[country_id]" id="user_country_id"><option value="">Select a Country</option>
<option alpha_code="AFG" value="244">Afghanistan</option>
<option alpha_code="ALA" value="4">Åland Islands</option>
<option alpha_code="ALB" value="240">Albania</option>
<option alpha_code="DZA" value="8">Algeria</option>
<option alpha_code="ASM" value="243">American Samoa</option>
<option alpha_code="AND" value="246">Andorra</option>
<option alpha_code="AGO" value="241">Angola</option>
<option alpha_code="AIA" value="5">Anguilla</option>
<option alpha_code="ATA" value="245">Antarctica</option>
<option alpha_code="ATG" value="242">Antigua and Barbuda</option>
<option alpha_code="ARG" value="1">Argentina</option>
<option alpha_code="ARM" value="9">Armenia</option>
<option alpha_code="ABW" value="2">Aruba</option>
<option alpha_code="AUS" value="7">Australia</option>
<option alpha_code="AUT" value="6">Austria</option>
<option alpha_code="AZE" value="3">Azerbaijan</option>
<option alpha_code="BHS" value="10">Bahamas</option>
<option alpha_code="BHR" value="11">Bahrain</option>
<option alpha_code="BGD" value="13">Bangladesh</option>
<option alpha_code="BRB" value="12">Barbados</option>
<option alpha_code="BLR" value="14">Belarus</option>
<option alpha_code="BEL" value="15">Belgium</option>
<option alpha_code="BLZ" value="16">Belize</option>
<option alpha_code="BEN" value="17">Benin</option>
<option alpha_code="BMU" value="18">Bermuda</option>
<option alpha_code="BTN" value="19">Bhutan</option>
<option alpha_code="BOL" value="20">Bolivia (Plurinational State of)</option>
<option alpha_code="BES" value="21">Bonaire, Sint Eustatius and Saba</option>
<option alpha_code="BIH" value="22">Bosnia and Herzegovina</option>
<option alpha_code="BWA" value="23">Botswana</option>
<option alpha_code="BRA" value="24">Brazil</option>
<option alpha_code="IOT" value="25">British Indian Ocean Territory</option>
<option alpha_code="BRN" value="28">Brunei Darussalam</option>
<option alpha_code="BGR" value="29">Bulgaria</option>
<option alpha_code="BFA" value="30">Burkina Faso</option>
<option alpha_code="BDI" value="31">Burundi</option>
<option alpha_code="CPV" value="35">Cabo Verde</option>
<option alpha_code="KHM" value="32">Cambodia</option>
<option alpha_code="CMR" value="33">Cameroon</option>
<option alpha_code="CAN" value="34">Canada</option>
<option alpha_code="CYM" value="36">Cayman Islands</option>
<option alpha_code="CAF" value="38">Central African Republic</option>
<option alpha_code="TCD" value="39">Chad</option>
<option alpha_code="CHL" value="37">Chile</option>
<option alpha_code="CHN" value="40">China</option>
<option alpha_code="CXR" value="41">Christmas Island</option>
<option alpha_code="CCK" value="42">Cocos (Keeling) Islands</option>
<option alpha_code="COL" value="43">Colombia</option>
<option alpha_code="COM" value="44">Comoros</option>
<option alpha_code="COG" value="45">Congo</option>
<option alpha_code="COD" value="46">Congo (Democratic Republic of the)</option>
<option alpha_code="COK" value="47">Cook Islands</option>
<option alpha_code="CRI" value="48">Costa Rica</option>
<option alpha_code="CIV" value="96">Côte d'Ivoire</option>
<option alpha_code="HRV" value="49">Croatia</option>
<option alpha_code="CUB" value="50">Cuba</option>
<option alpha_code="CUW" value="51">Curaçao</option>
<option alpha_code="CYP" value="52">Cyprus</option>
<option alpha_code="CZE" value="53">Czech Republic</option>
<option alpha_code="DNK" value="54">Denmark</option>
<option alpha_code="DJI" value="55">Djibouti</option>
<option alpha_code="DMA" value="56">Dominica</option>
<option alpha_code="DOM" value="57">Dominican Republic</option>
<option alpha_code="ECU" value="58">Ecuador</option>
<option alpha_code="EGY" value="59">Egypt</option>
<option alpha_code="SLV" value="60">El Salvador</option>
<option alpha_code="GNQ" value="61">Equatorial Guinea</option>
<option alpha_code="ERI" value="62">Eritrea</option>
<option alpha_code="EST" value="63">Estonia</option>
<option alpha_code="ETH" value="64">Ethiopia</option>
<option alpha_code="FLK" value="65">Falkland Islands (Malvinas)</option>
<option alpha_code="FRO" value="66">Faroe Islands</option>
<option alpha_code="FJI" value="67">Fiji</option>
<option alpha_code="FIN" value="68">Finland</option>
<option alpha_code="FRA" value="69">France</option>
<option alpha_code="GUF" value="70">French Guiana</option>
<option alpha_code="PYF" value="71">French Polynesia</option>
<option alpha_code="GAB" value="72">Gabon</option>
<option alpha_code="GMB" value="73">Gambia</option>
<option alpha_code="GEO" value="74">Georgia</option>
<option alpha_code="DEU" value="75">Germany</option>
<option alpha_code="GHA" value="76">Ghana</option>
<option alpha_code="GIB" value="77">Gibraltar</option>
<option alpha_code="GRC" value="78">Greece</option>
<option alpha_code="GRL" value="79">Greenland</option>
<option alpha_code="GRD" value="80">Grenada</option>
<option alpha_code="GLP" value="81">Guadeloupe</option>
<option alpha_code="GUM" value="82">Guam</option>
<option alpha_code="GTM" value="83">Guatemala</option>
<option alpha_code="GGY" value="84">Guernsey</option>
<option alpha_code="GIN" value="85">Guinea</option>
<option alpha_code="GNB" value="86">Guinea-Bissau</option>
<option alpha_code="GUY" value="87">Guyana</option>
<option alpha_code="HTI" value="88">Haiti</option>
<option alpha_code="VAT" value="89">Holy See</option>
<option alpha_code="HND" value="90">Honduras</option>
<option alpha_code="HKG" value="91">Hong Kong</option>
<option alpha_code="HUN" value="92">Hungary</option>
<option alpha_code="ISL" value="93">Iceland</option>
<option alpha_code="IND" value="94">India</option>
<option alpha_code="IDN" value="95">Indonesia</option>
<option alpha_code="IRN" value="97">Iran (Islamic Republic of)</option>
<option alpha_code="IRQ" value="98">Iraq</option>
<option alpha_code="IRL" value="99">Ireland</option>
<option alpha_code="IMN" value="100">Isle of Man</option>
<option alpha_code="ISR" value="101">Israel</option>
<option alpha_code="ITA" value="102">Italy</option>
<option alpha_code="JAM" value="103">Jamaica</option>
<option alpha_code="JPN" value="104">Japan</option>
<option alpha_code="JEY" value="105">Jersey</option>
<option alpha_code="JOR" value="106">Jordan</option>
<option alpha_code="KAZ" value="107">Kazakhstan</option>
<option alpha_code="KEN" value="108">Kenya</option>
<option alpha_code="KIR" value="109">Kiribati</option>
<option alpha_code="PRK" value="155">Korea (Democratic People's Republic of)</option>
<option alpha_code="KOR" value="200">Korea (Republic of)</option>
<option alpha_code="KWT" value="110">Kuwait</option>
<option alpha_code="KGZ" value="111">Kyrgyzstan</option>
<option alpha_code="LAO" value="112">Lao People's Democratic Republic</option>
<option alpha_code="LVA" value="113">Latvia</option>
<option alpha_code="LBN" value="114">Lebanon</option>
<option alpha_code="LSO" value="115">Lesotho</option>
<option alpha_code="LBR" value="116">Liberia</option>
<option alpha_code="LBY" value="117">Libya</option>
<option alpha_code="LIE" value="118">Liechtenstein</option>
<option alpha_code="LTU" value="119">Lithuania</option>
<option alpha_code="LUX" value="120">Luxembourg</option>
<option alpha_code="MAC" value="121">Macao</option>
<option alpha_code="MKD" value="122">Macedonia (the former Yugoslav Republic of)</option>
<option alpha_code="MDG" value="123">Madagascar</option>
<option alpha_code="MWI" value="124">Malawi</option>
<option alpha_code="MYS" value="125">Malaysia</option>
<option alpha_code="MDV" value="126">Maldives</option>
<option alpha_code="MLI" value="127">Mali</option>
<option alpha_code="MLT" value="128">Malta</option>
<option alpha_code="MHL" value="129">Marshall Islands</option>
<option alpha_code="MTQ" value="130">Martinique</option>
<option alpha_code="MRT" value="131">Mauritania</option>
<option alpha_code="MUS" value="132">Mauritius</option>
<option alpha_code="MYT" value="133">Mayotte</option>
<option alpha_code="MEX" value="134">Mexico</option>
<option alpha_code="FSM" value="135">Micronesia (Federated States of)</option>
<option alpha_code="MDA" value="136">Moldova (Republic of)</option>
<option alpha_code="MCO" value="137">Monaco</option>
<option alpha_code="MNG" value="138">Mongolia</option>
<option alpha_code="MNE" value="139">Montenegro</option>
<option alpha_code="MSR" value="140">Montserrat</option>
<option alpha_code="MAR" value="141">Morocco</option>
<option alpha_code="MOZ" value="142">Mozambique</option>
<option alpha_code="MMR" value="143">Myanmar</option>
<option alpha_code="NAM" value="144">Namibia</option>
<option alpha_code="NRU" value="145">Nauru</option>
<option alpha_code="NPL" value="146">Nepal</option>
<option alpha_code="NLD" value="147">Netherlands</option>
<option alpha_code="NCL" value="148">New Caledonia</option>
<option alpha_code="NZL" value="149">New Zealand</option>
<option alpha_code="NIC" value="150">Nicaragua</option>
<option alpha_code="NER" value="151">Niger</option>
<option alpha_code="NGA" value="152">Nigeria</option>
<option alpha_code="NIU" value="153">Niue</option>
<option alpha_code="NFK" value="154">Norfolk Island</option>
<option alpha_code="MNP" value="156">Northern Mariana Islands</option>
<option alpha_code="NOR" value="157">Norway</option>
<option alpha_code="OMN" value="158">Oman</option>
<option alpha_code="PAK" value="159">Pakistan</option>
<option alpha_code="PLW" value="160">Palau</option>
<option alpha_code="PSE" value="161">Palestine, State of</option>
<option alpha_code="PAN" value="162">Panama</option>
<option alpha_code="PNG" value="163">Papua New Guinea</option>
<option alpha_code="PRY" value="164">Paraguay</option>
<option alpha_code="PER" value="165">Peru</option>
<option alpha_code="PHL" value="166">Philippines</option>
<option alpha_code="PCN" value="167">Pitcairn</option>
<option alpha_code="POL" value="168">Poland</option>
<option alpha_code="PRT" value="169">Portugal</option>
<option alpha_code="PRI" value="170">Puerto Rico</option>
<option alpha_code="QAT" value="171">Qatar</option>
<option alpha_code="KOS" value="172">Republic of Kosovo</option>
<option alpha_code="REU" value="173">Réunion</option>
<option alpha_code="ROU" value="174">Romania</option>
<option alpha_code="RUS" value="175">Russian Federation</option>
<option alpha_code="RWA" value="176">Rwanda</option>
<option alpha_code="BLM" value="177">Saint Barthélemy</option>
<option alpha_code="SHN" value="178">Saint Helena, Ascension and Tristan da Cunha</option>
<option alpha_code="KNA" value="179">Saint Kitts and Nevis</option>
<option alpha_code="LCA" value="180">Saint Lucia</option>
<option alpha_code="MAF" value="181">Saint Martin (French part)</option>
<option alpha_code="SPM" value="182">Saint Pierre and Miquelon</option>
<option alpha_code="VCT" value="183">Saint Vincent and the Grenadines</option>
<option alpha_code="WSM" value="184">Samoa</option>
<option alpha_code="SMR" value="185">San Marino</option>
<option alpha_code="STP" value="186">Sao Tome and Principe</option>
<option alpha_code="SAU" value="187">Saudi Arabia</option>
<option alpha_code="SEN" value="188">Senegal</option>
<option alpha_code="SRB" value="189">Serbia</option>
<option alpha_code="SYC" value="190">Seychelles</option>
<option alpha_code="SLE" value="191">Sierra Leone</option>
<option alpha_code="SGP" value="192">Singapore</option>
<option alpha_code="SXM" value="193">Sint Maarten (Dutch part)</option>
<option alpha_code="SVK" value="194">Slovakia</option>
<option alpha_code="SVN" value="195">Slovenia</option>
<option alpha_code="SLB" value="196">Solomon Islands</option>
<option alpha_code="SOM" value="197">Somalia</option>
<option alpha_code="ZAF" value="198">South Africa</option>
<option alpha_code="SGS" value="199">South Georgia and the South Sandwich Islands</option>
<option alpha_code="SSD" value="201">South Sudan</option>
<option alpha_code="ESP" value="202">Spain</option>
<option alpha_code="LKA" value="203">Sri Lanka</option>
<option alpha_code="SDN" value="204">Sudan</option>
<option alpha_code="SUR" value="205">Suriname</option>
<option alpha_code="SJM" value="206">Svalbard and Jan Mayen</option>
<option alpha_code="SWZ" value="207">Swaziland</option>
<option alpha_code="SWE" value="208">Sweden</option>
<option alpha_code="CHE" value="209">Switzerland</option>
<option alpha_code="SYR" value="210">Syrian Arab Republic</option>
<option alpha_code="TWN" value="211">Taiwan</option>
<option alpha_code="TJK" value="212">Tajikistan</option>
<option alpha_code="TZA" value="213">Tanzania, United Republic of</option>
<option alpha_code="THA" value="214">Thailand</option>
<option alpha_code="TLS" value="215">Timor-Leste</option>
<option alpha_code="TGO" value="216">Togo</option>
<option alpha_code="TKL" value="217">Tokelau</option>
<option alpha_code="TON" value="218">Tonga</option>
<option alpha_code="TTO" value="219">Trinidad and Tobago</option>
<option alpha_code="TUN" value="220">Tunisia</option>
<option alpha_code="TUR" value="221">Turkey</option>
<option alpha_code="TKM" value="222">Turkmenistan</option>
<option alpha_code="TCA" value="223">Turks and Caicos Islands</option>
<option alpha_code="TUV" value="224">Tuvalu</option>
<option alpha_code="UGA" value="225">Uganda</option>
<option alpha_code="UKR" value="226">Ukraine</option>
<option alpha_code="ARE" value="227">United Arab Emirates</option>
<option alpha_code="GBR" value="228">United Kingdom of Great Britain and Northern Ireland</option>
<option alpha_code="USA" value="229">United States of America</option>
<option alpha_code="URY" value="230">Uruguay</option>
<option alpha_code="UZB" value="231">Uzbekistan</option>
<option alpha_code="VUT" value="232">Vanuatu</option>
<option alpha_code="VEN" value="233">Venezuela (Bolivarian Republic of)</option>
<option alpha_code="VNM" value="234">Viet Nam</option>
<option alpha_code="VGB" value="26">Virgin Islands (British)</option>
<option alpha_code="VIR" value="27">Virgin Islands (U.S.)</option>
<option alpha_code="WLF" value="235">Wallis and Futuna</option>
<option alpha_code="ESH" value="236">Western Sahara</option>
<option alpha_code="YEM" value="237">Yemen</option>
<option alpha_code="ZMB" value="238">Zambia</option>
<option alpha_code="ZWE" value="239">Zimbabwe</option></select>
</div><div class="input string required user_address_location"><label class="string required" for="user_address"><abbr title="required">*</abbr> Location</label><input id="user_address" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][location]"></div>
<div class="input string required user_address_street_name"><label class="string required" for="user_address_attributes_street_name"><abbr title="required">*</abbr> Street name</label><input user-data-geo="route" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][street_name]" id="user_address_attributes_street_name"></div>
<div class="input string required user_address_street_number"><label class="string required" for="user_address_attributes_street_number"><abbr title="required">*</abbr> Street number</label><input user-data-geo="street_number" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][street_number]" id="user_address_attributes_street_number"></div>
<div class="input string required user_address_city"><label class="string required" for="user_address_attributes_city"><abbr title="required">*</abbr> City</label><input user-data-geo="locality" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][city]" id="user_address_attributes_city"></div>
<div class="input string required user_address_state"><label class="string required" for="user_address_attributes_state"><abbr title="required">*</abbr> State</label><input user-data-geo="administrative_area_level_1" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][state]" id="user_address_attributes_state"></div>
<div class="input string required user_address_postal_code"><label class="string required" for="user_address_attributes_postal_code"><abbr title="required">*</abbr> Postal code</label><input user-data-geo="postal_code" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][postal_code]" id="user_address_attributes_postal_code"></div>
<div class="input string required user_address_administrative_area_level_1"><label class="string required" for="user_address_attributes_administrative_area_level_1"><abbr title="required">*</abbr> Administrative area level 1</label><input user-data-geo="floor" class="string required" required="required" aria-required="true" type="text" name="user[address_attributes][administrative_area_level_1]" id="user_address_attributes_administrative_area_level_1"></div>
<div class="input string optional user_address_latitude"><label class="string optional" for="user_address_attributes_latitude">Latitude</label><input user-data-geo="lat" class="string optional" type="text" name="user[address_attributes][latitude]" id="user_address_attributes_latitude"></div>
<div class="input string optional user_address_longitude"><label class="string optional" for="user_address_attributes_longitude">Longitude</label><input user-data-geo="lng" class="string optional" type="text" name="user[address_attributes][longitude]" id="user_address_attributes_longitude"></div>
<input type="submit" name="commit" value="Create User" data-disable-with="Create User">
</form>
4

0 回答 0