這應該不是那么困難... Javascript 和 jQuery 有點生疏,我想我錯過了一些明顯的東西。
代碼示例。縮寫 DOM
<select data-drupal-selector="edit-country-code-country" id="edit-country-code-country" name="country_code[country]"
class="form-select is-empty">
<option value="" selected="selected">- None -</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="ZW">Zimbabwe</option>
<option value="AX">?land Islands</option>
</select>
腳本中的代碼(不起作用)
$('select[id^="edit-country-code-country"]').on('change', () => {
const $target = $('#opt-in-wrapper');
const countryNames = drupalSettings.r1;
const $countrySelected = $('#select2-edit-country-code-country-container').attr('title');
const countryCode = this.value;
const test1 = $(this).value;
const this2 = $(this).change;
const this3 = $(this).data();
const elem = $(this);
const test5 = elem.value;
const test6 = elem.value();
我不明白的是為什么this.value在腳本中回傳 undefined 但在除錯控制臺中按我的預期作業
除錯結果截圖。

uj5u.com熱心網友回復:
如果元素有id,您可以只使用id查詢選擇器。然后,您可以找到所選的選項。
$('#edit-country-code-country').change(function() {
var $option = $(this).find('option:selected');
var code = $option.val(); // to get selected country code
var name = $option.text(); // to get selected country name
});
uj5u.com熱心網友回復:
用函式運算式替換箭頭函式:
$('#edit-country-code-country').on('change', function(){
alert(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select data-drupal-selector="edit-country-code-country" id="edit-country-code-country" name="country_code[country]"
class="form-select is-empty">
<option value="" selected="selected">- None -</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="ZW">Zimbabwe</option>
<option value="AX">?land Islands</option>
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379259.html
標籤:javascript 查询
上一篇:如何將我的代碼修改為異步?
