从jQuery中的数据列表中获取值和文本 [英] Getting the value and text from a datalist in jquery
本文介绍了从jQuery中的数据列表中获取值和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取#place输入中的值并将其保留在该输入中,但是传真号码是否已进入#fax输入中?是否可以同时获得两个值并将其放入相应的位置.
I am trying to get the value in the #place input and keep it in that input but have the fax number go into the #fax input? Is it possible to get both values and drop them into the respective locations.
input type="text" id="place" list="places">
<datalist id="places">
<option value="WVC">503-882-1212</option>
<option value="HAM">612-883-1414</option>
<option value="WON">317-445-8585</option>
</datalist>
<br>
<input type="text" id="fax">
推荐答案
尝试使用jquery attr值
try using jquery attr value
$(document).on('change', '#place', function () {
$("#fax").val($("#places option[value='" + $("#place").val() + "']").attr("label"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="place" list="places">
<datalist id="places">
<option value="WVC" label="503-882-1212"></option>
<option value="HAM" label="612-883-1414"></option>
<option value="WON" label="317-445-8585"></option>
</datalist>
<br>
<input type="text" id="fax">
这篇关于从jQuery中的数据列表中获取值和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文