Javascript 更改div';通过选择';s选项
我必须编写一些代码,其中在select中有三个选项,div的值在更改时将更改为所选项目的值。我尝试了很多方法和函数,但都不起作用。这是我当前的代码:Javascript 更改div';通过选择';s选项,javascript,html,Javascript,Html,我必须编写一些代码,其中在select中有三个选项,div的值在更改时将更改为所选项目的值。我尝试了很多方法和函数,但都不起作用。这是我当前的代码: $('select[class=“country”]”)。在('change',函数(){ var get=$('select option:selected').text(); $('#fDiv')。text=get; }); 最酷的国家 韩国 中国 罗马尼亚 使用.text()如:$('#fDiv').text(myText) $('sel
$('select[class=“country”]”)。在('change',函数(){
var get=$('select option:selected').text();
$('#fDiv')。text=get;
});代码>
最酷的国家
韩国
中国
罗马尼亚
使用.text()
如:$('#fDiv').text(myText)代码>
$('select[class=“country”]”)。在('change',function()上{
var myText=$('select option:selected')。text();
$('#fDiv').text(myText);
});代码>
最酷的国家
韩国
中国
罗马尼亚
使用.text()
如:$('#fDiv').text(myText)代码>
$('select[class=“country”]”)。在('change',function()上{
var myText=$('select option:selected')。text();
$('#fDiv').text(myText);
});代码>
最酷的国家
韩国
中国
罗马尼亚
这是一个针对您的问题的纯javascript解决方案
const select=document.querySelector(“#country”);
const div=document.querySelector('#fDiv');
div.innerHTML=select.selectedOptions[0]。值//如果最初不想设置值,请删除此选项
select.addEventListener('change',(事件)=>{
div.innerHTML=select.selectedOptions[0]。值
});代码>
最酷的国家
韩国
中国
罗马尼亚
这是一个针对您的问题的纯javascript解决方案
const select=document.querySelector(“#country”);
const div=document.querySelector('#fDiv');
div.innerHTML=select.selectedOptions[0]。值//如果最初不想设置值,请删除此选项
select.addEventListener('change',(事件)=>{
div.innerHTML=select.selectedOptions[0]。值
});代码>
最酷的国家
韩国
中国
罗马尼亚
对于jQuery,您不使用赋值=,而是像(“#id”)一样使用它。text(get):
$('select[class=“country”]”)。在('change',function()上{
var get=$('select option:selected').val();
$('#fDiv').text(get);
});代码>
最酷的国家
韩国
中国
罗马尼亚
对于jQuery,您不使用赋值=,而是像(“#id”)一样使用它。text(get):
$('select[class=“country”]”)。在('change',function()上{
var get=$('select option:selected').val();
$('#fDiv').text(get);
});代码>
最酷的国家
韩国
中国
罗马尼亚
1)在文件开头添加
2) 在name=“country”id=“country”
3) 将text()与参数一起使用:例如$('#fDiv').text(get)代码>
$('select[class=“country”]”)。在('change',函数(){
var get=$('select option:selected').text();
$('#fDiv').text(get);
});代码>
酷站点
最酷的国家
韩国
中国
罗马尼亚
1)在文件开头添加
2) 在name=“country”id=“country”
3) 将text()与参数一起使用:例如$('#fDiv').text(get)代码>
$('select[class=“country”]”)。在('change',函数(){
var get=$('select option:selected').text();
$('#fDiv').text(get);
});代码>
酷站点
最酷的国家
韩国
中国
罗马尼亚