我已经搜索了很长一段时间,但似乎无法找到全面的答案。我有下面的 HTML 代码,并想将输入的值添加input name="ytcustom"
到option value="https://url.com/custom/"
什么是实现这一目标的可靠方法?
<select id="youtube_dropdown">
<option value="https://url.com/mix/">Mix</option>
<option value="https://url.com/custom/">Custom</option>
</select>
<input name="ytcustom" id="ytcustom" type="text" class="ythide">
这是我的答案的最终版本:使用onkeyup
事件并使用#youtube_dropdown > option:last
来选择一个选项元素(尽管我会尽可能使用 ID 选择器):
$(function() {
$("#ytcustom").on("input", function() {
var customUrl = 'https://url.com/custom/' + $(this).val();
$("#youtube_dropdown > option:last").val(customUrl);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="youtube_dropdown">
<option value="https://url.com/mix/">Mix</option>
<option value="https://url.com/custom/">Custom</option>
</select>
<input name="ytcustom" id="ytcustom" type="text" class="ythide">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句