以jQueryforms出现.change函数问题
我正在编写一个脚本,根据选择菜单中的用户输入更改范围文本值。
我的当前脚本有问题。 如何修复它,以便当我单击“结束日期”然后再次单击“开始日期”时,它会在跨度中显示“开始日期”?
演示: http : //jsfiddle.net/197ncb10/
$(function() { $('#usertype').change(function() { $(this).next("span").text("End Date"); }); });
这样做
$(function() { $('#usertype').change(function() { $(this).next("span").text($('#usertype option:selected').text()); }); });
像这样:
$(function() { $('#usertype').change(function() { let text=$(':selected',this).text(); $(this).next("span").text(text); }); });
您需要确定所选选项,然后确定该选项中的文本。
$(function() { $('#usertype').change(function() { var selectedText = $("#usertype option[value='"+ $(this).val() +"']").text(); $(this).next("span").text(selectedText); }); });
看看这个jsfiddle JSFIDDLE
希望这会帮助你。
为span提供id,如:
jQuery的:
$(function() { $('#usertype').change(function() { $('#span_text').val($('#usertype option:selected').text()); }); });