[英]Multiple select option selected will be view in another html element using javascript or jquery
I have a multiple select option on html like this : 我在html上有多个选择选项,如下所示:
<div class="control-group">
<label class="control-label" for="selectError1">Ditujukan untuk :</label>
<div class="controls">
<select id="email" multiple data-rel="chosen" class="input-xlarge" name="email[]">
<?php
foreach ($atasan as $data) {
echo "<option value='" . $data['email'] . "'>" . $data['email'] . "</option>";
}
?>
</select>
</div>
</div>
My goal is, when user selected an or any option, the option that selected will be view in another html element like <p id='emailSend'>This is the email : email1, email2... so on</p
. 我的目标是,当用户选择一个或任何选项时,所选的选项将在另一个HTML元素(例如
<p id='emailSend'>This is the email : email1, email2... so on</p
。
I am newbie in jquery. 我是jquery的新手。 I guess 'on change' or change can do this.
我猜“改变”或改变都可以做到。
This is the jquery's code: 这是jquery的代码:
$(document).ready(function() {
$('#emailSend').hide();
$('email').change(function(){
//any idea ???
});
}
EDIT I was wondering to increase my ability, how should I do if I want to append it on a input text like 编辑我想增加自己的能力,如果我想将其附加在输入文本上,该怎么办
<input type="text" id="emailSend" name="emailSend">
So, the jquery looked like this : 因此,jQuery看起来像这样:
$("#email").change(function() {
var elements = $("#email option:selected").length;
$(".emailSend").html("");
$.each($("#email option:selected"), function(index, element) {
$(".emailSend").val($(this).html());
if (index < elements - 1) {
$(".emailSend").append(", ");
}
});
});
What you can do is : 您可以做的是:
$("#email").change(function() {
var elements = $("#email option:selected").length;
$("#emailSend").html("");
$.each($("#email option:selected"), function(index, element){
$("#emailSend").append($(this).html());
if (index < elements-1) {
$("#emailSend").append(", ");
}
});
});
With emailSend
being your block containing all the emails. 随着
emailSend
成为您包含所有电子邮件的阻止。 Note that this solution allows you to handle multiple values. 请注意,此解决方案允许您处理多个值。
EDIT : grammar :( 编辑:语法:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.