1 回答

TA貢獻1998條經驗 獲得超6個贊
獲取選中的選項索引?HTMLSelectElement/selectedIndex
使用SelectElement.options[?index?]定位 OPTION 元素
使用 jQuery 的.closest()?方法引用公共 TR 父級
使用 jQuery 的.find()方法查找元素
$('.decisionList').on("change", function() {
? const i = this.selectedIndex;
? $(this).closest("tr").find(".choice").val(this.options[i].textContent);
});
<table>
? <tr>
? ? <td>111</td>
? ? <td>Anna</td>
? ? <td>
? ? ? <select class="decisionList">
? ? ? ? <option selected value="b"></option>
? ? ? ? <option value="n">None</option>
? ? ? ? <option value="c">Cancellation</option>
? ? ? ? <option value="d">Date of payment</option>
? ? ? </select>
? ? </td>
? ? <td><input class="choice" name="111" type="text"></td>
? </tr>
? <tr>
? ? <td>222</td>
? ? <td>John</td>
? ? <td>
? ? ? <select class="decisionList">
? ? ? ? <option selected value="b"></option>
? ? ? ? <option value="n">None</option>
? ? ? ? <option value="c">Cancellation</option>
? ? ? ? <option value="d">Date of payment</option>
? ? ? </select>
? ? </td>
? ? <td><input class="choice" name="222" type="text"></td>
? </tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
或者以更 jQuery 的方式使用這種方式:
$('.decisionList').on("change", function() {
? const $option = $(this).find(":selected");
? $(this).closest("tr").find(".choice").val($option.text());
});
添加回答
舉報