我有一個下拉選單和兩個單選按鈕,兩者都有兩個值:
jQuery('input[type=radio][name=radio_type]').change(function(e) {
var value = jQuery(e.target.value);
jQuery('select[name="optType"] option:selected').attr("selected", null);
jQuery('select[name="optType"] option[value=' value ']').attr("selected", "selected");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" value="1" name="radio_type">
<input type="radio" value="2" name="radio_type">
<select name="optType" id="optType" class="form-control">
<option value="0">Select</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
我想從下拉串列中選擇正確的選項——與單選具有相同值的選項——單擊單選按鈕;例如:如果我單擊值為 1 的單選按鈕,它應該選擇值為 1 的下拉選項。
我正在使用上面的腳本,但它不起作用。
uj5u.com熱心網友回復:
無需在第 2 行將 e.target.value 包裝到 jQuery 即。jQuery(e.target.value)。
使用 - var value = e.target.value;
它會起作用的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418581.html
標籤:
