大家好我有以下代码,在这里工作演示:
https://jsfiddle.net/qf28f9fd/21/
$(document).ready(function() {
$('#one').change(function() {
var selected = $(this).find('option:selected').text();
console.log('selected', selected);
$("#writer").append(selected);
});
})
<p class="Title">
<h2 id="writer">MARIO PUZO'S</h2>
<h1 id="godfathertitle">THE GODFATHER</h1>
<h1 id="shawshanktitle" style="display: none;">Shawshank Redemption</h1>
<h1 id="fightclubtitle" style="display: none;">Fight Club</h1>
<h4>by Mario Puzo and Francis Ford Coppola</h4> TRANSCRIPT
</p>
<div id="image">
</div>
<div class="Txt2">
ADE FROM BLACK: Int. of Don Corleone's home office -day
<br>
<br> BONASERA
<font style="font-style:italic" ;>(seated in front of the Dons
<select id="one">
<option value="Paolo Peters">desk</option>
<option value="Shirin Grand">dark, empty room(shawshank redemption)</option>
<option value ="Jeroen Pronk">SOCIAL ROOM(fight club)</option>
</select>, facing the camera)</font>
<br>
<div id="partshawshank2" style="display:none">shawshank</div>
<div id="partfightclub2" style="display:none">shawshank</div>
<p id="demo"></p>
<br>
我将选项中的文本添加到div中。
但是,除了添加文本,我想添加值,有人可以帮我吗?我正在努力获得价值。提前致谢
答案 0 :(得分:1)
我解决了你的问题。只需更改以下代码:
var selected = $(this).find('option:selected').text();
为:
var selected = document.getElementById("one").value;
答案 1 :(得分:0)
更改此行
var selected = $(this).find('option:selected').text();
要
var selected = $(this).find('option:selected').val();
答案 2 :(得分:0)
使用以下代码替换您的脚本:
$(document).ready(function() {
$('#one').change(function() {
var selected = $('#one').val();
$("#writer").append(selected);
});
})
答案 3 :(得分:0)
将脚本代码替换为:
$(document).ready(function() {
$('#one').change(function() {
var selected = $(this).find('option:selected').val();
console.log('selected', selected);
$("#writer").html("").append(selected);
});
})
答案 4 :(得分:0)
使用var selected = $(this).find('option:selected').val();
.val()
方法主要用于获取表单元素的值,例如input
,select
和textarea
。
请参阅.val()
也可以使用<option selected="true" disabled="disabled">Choose Room</option>
作为默认选择选项,因为在您选择第一个选项(暗)的情况下,不会获得任何结果。
$(document).ready(function() {
var titles = ["THE GODFATHER", "Shawshank Redemption", "Fight Club"];
$('#one').change(function() {
var id = $(this).children(":selected").attr("id");
var selected = $(this).find('option:selected').val();
$("#title").html(titles[id]);
$("#writer").html(selected);
});
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="Title">
<h2 id="writer">MARIO PUZO'S</h2>
<h1 id="title">THE GODFATHER</h1>
<h4>by Mario Puzo and Francis Ford Coppola</h4> TRANSCRIPT
</p>
<div id="image">
</div>
<div class="Txt2">
ADE FROM BLACK: Int. of Don Corleone's home office -day
<br>
<br> BONASERA
<font style="font-style:italic" ;>(seated in front of the Dons
<select id="one">
<option selected="true" disabled="disabled">Choose Room</option>
<option id=0 value="Paolo Peters">desk</option>
<option id= 1 value="Shirin Grand">dark, empty room(shawshank redemption)</option>
<option id =2 value ="Jeroen Pronk">SOCIAL ROOM(fight club)</option>
</select>, facing the camera)</font>
<br>
<div id="partshawshank2" style="display:none">shawshank</div>
<div id="partfightclub2" style="display:none">shawshank</div>
<p id="demo"></p>
&#13;