我试图从下拉列表中获取所选选项,并用该文本填充另一个项目,如下所示.IE正在掀起风暴,但它在Firefox中不起作用:
$('#ddlCodes').change(function() {
$('#txtEntry2').text('#ddlCodes option:selected').text();
});
我做错了什么?
我试图从下拉列表中获取所选选项,并用该文本填充另一个项目,如下所示.IE正在掀起风暴,但它在Firefox中不起作用:
$('#ddlCodes').change(function() {
$('#txtEntry2').text('#ddlCodes option:selected').text();
});
我做错了什么?
Here's a short version:
$('#ddlCodes').change(function() {
$('#txtEntry2').text($(this).find(":selected").text());
});
karim79是个不错的 Select ,从您的元素名称判断,txtEntry2
可能是一个文本框,如果它是任何类型的输入,您将需要使用.val()
来代替,或者使用.text()
,如下所示:
$('#txtEntry2').val($(this).find(":selected").text());
For the "what's wrong?" part of the question: .text()
doesn't take a selector, it takes text you want it set to, or nothing to return the text already there. So you need to fetch the text you want, then put it in the .text(string)
method on the object you want to set, like I have above.