簡體   English   中英

更改所選選項的值

[英]Change value of selected option

我有下拉 select 選項國家:

    <select name="country" id="countryId" required="required"
class="countries order-alpha form-control custom-select bg-white border-left-0 border-md">
                                <option value="">Select Country</option>
                                <option value="1"> Afghanistan</option>
                                <option value="2"> Aland Islands</option>
                                <option value="3"> Albania</option>
                                <option value="4"> Algeria</option>
                                <option value="5"> American Samoa</option>

當我更改 select 國家/地區時,我將值(數字)發送到服務器並獲取在州中解析的州列表與城市相同。 填寫完所有字段后,formData.serialized 並發送到服務器。 服務器獲取國家、state 和城市的數字 id 但我想發送國家/州/城市的名稱。 所以在發送之前我應該將選定的值更改為選定的文本(國家名稱)。 我嘗試這樣改變:

$("#countryId").change(function () {
 c.countrytxt = $(this).find("option:selected").text(); // get text from option
 var countryId = $("#countryId").val(); // save for send as paramet to server
 document.getElementById('countryId').value = c.countrytxt; // try set county name to value

我怎樣才能實現它?

請改用這個。

 $("#countryId").change(function () { // get text from option countrytxt = $(this).find("option:selected").text(); // save for send as paramet to server var countryId = $("#countryId").val(); // try set county name to value $(this).find("option:selected").attr('value',countrytxt); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="country" id="countryId" required="required" class="countries order-alpha form-control custom-select bg-white border-left-0 border-md"> <option value="">Select Country</option> <option value="1"> Afghanistan</option> <option value="2"> Aland Islands</option> <option value="3"> Albania</option> <option value="4"> Algeria</option> <option value="5"> American Samoa</option> </select>

暫無
暫無

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

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