如何将一个值从下拉列表追加到输入框的末尾? 特此附上我的守则。我想在收入输入框的末尾添加货币符号[在下拉列表中选择的任何一个]。
<html>
<head>
<meta charset="UTF-8">
<title> Stack query</title>
<style>
table{
border: 1px solid black;
height:20%;
width:20%;
margin: 5%;
}
table tr td label, table tr td input,table tr td select {
width:100%;
}
</style>
</head>
<body>
<div>
<table>
<tr>
<td><label for="Currency">Currency</label></td>
<td>
<select name="currency" id="currency">
<option value="Select" selected disabled="disabled">Select Currency</option>
<option value="Dollar">$ USD</option>
<option value="Pound">£ GBP</option>
<option value="Euro">€ EUR</option>
<option value="Yen">¥ YEN</option>
</select>
</td>
</tr>
<tr>
<td><label for="Revenue">Revenue</label></td>
<td><input id="Revenue" type="text" required></td>
</tr>
</table>
</div>
</body>
</html>
答案 0 :(得分:1)
您可以使用<span>
标记显示货币符号,如下所示:
$('#currency').on('change', function() {
$('#symbol').text(this.options[this.selectedIndex].innerText.charAt(0));
// or the jQuery version
//$('#symbol').text($(this).find('option:selected').text().charAt(0));
});
&#13;
table {
border: 1px solid black;
height: 20%;
width: 40%;
margin: 5%;
}
table tr td label,
table tr td input,
table tr td select {
width: 80%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<table>
<tr>
<td>
<label for="Currency">Currency</label>
</td>
<td>
<select name="currency" id="currency">
<option value="Select" selected disabled="disabled">Select Currency</option>
<option value="Dollar">$ USD</option>
<option value="Pound">£ GBP</option>
<option value="Euro">€ EUR</option>
<option value="Yen">¥ YEN</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="Revenue">Revenue</label>
</td>
<td>
<input id="Revenue" type="text" required>
<span id="symbol"></span>
</td>
</tr>
</table>
</div>
&#13;
要更新文本框值本身,请尝试以下操作:
$('#currency').on('change', function() {
var selected;
var symbols = $(this.options).map(function(option) {
if (this.selected)
selected = this.innerText.charAt(0);
return this.innerText.charAt(0);
}).get();
$('#Revenue').val(function(i, val) {
if (!val) return
return symbols.indexOf(val.slice(-1)) >= 0 ?
val.slice(0, -1) + selected : val + selected;
});
});
&#13;
table {
border: 1px solid black;
height: 20%;
width: 40%;
margin: 5%;
}
table tr td label,
table tr td input,
table tr td select {
width: 80%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<table>
<tr>
<td>
<label for="Currency">Currency</label>
</td>
<td>
<select name="currency" id="currency">
<option value="Select" selected disabled="disabled">Select Currency</option>
<option value="Dollar">$ USD</option>
<option value="Pound">£ GBP</option>
<option value="Euro">€ EUR</option>
<option value="Yen">¥ YEN</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="Revenue">Revenue</label>
</td>
<td>
<input id="Revenue" type="text" required>
<span id="symbol"></span>
</td>
</tr>
</table>
</div>
&#13;
答案 1 :(得分:0)
使用
change
事件。
$('#currency').on('change', function() {
$('#toAppend').text(this.value);
});
&#13;
table {
border: 1px solid black;
//height: 20%;
width: 50%;
margin: 5%;
}
table tr td label,
table tr td input,
table tr td select {
width: 100%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>
<table>
<tr>
<td>
<label for="Currency">Currency</label>
</td>
<td>
<select name="currency" id="currency">
<option value="Select" selected disabled="disabled">Select Currency</option>
<option value="Dollar">$ USD</option>
<option value="Pound">£ GBP</option>
<option value="Euro">€ EUR</option>
<option value="Yen">¥ YEN</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="Revenue" id='toAppend'>Revenue</label>
</td>
<td>
<!-- Id added -->
<input id="Revenue" type="text" required>
</td>
</tr>
</table>
</div>
&#13;