[英]How can I ignore previous selection and write only last selected
The html code is; html代码是;
<div>
<label>Categories
<select id="projectSelect" class="list_who_to_mail" name="projectSelect"></select>
</label>
</div>
</div>
</div>
<input name="projectID" id="projectID" type="hidden"></input>
<div>
<div>
<label >Discipline
<select name="disciplineSelect" id="disciplineSelect" class="list_who_to_mail"> </select>
</label>
</div>
</div>
</div>
<input name="categoryID" id="categoryID" type="hidden"></input>
<div>
<div>
<label >Main Group
<select name="main" id="main" class="list_who_to_mail">
<option></option>
<option>Technical</option>
<option>Contracts</option>
<option>Procurement / Logistics</option>
<option>Administrative</option>
<option>Financial</option>
<option>Legal</option>
</select>
</label>
</div>
</div>
</div>
<div >
<div >
<label>Effect on (Part 1)
<select name="observedPhase[]" id="observedPhase" multiple="multiple" class="list_who_to_mail" size="13">
<option>Warranty</option>
<option>Operability</option>
<option>Reliability</option>
<option>Reputation</option>
</select>
</h5></label>
</div>
</div>
</div>
the function is; 功能是;
$(function () {
$('.list_who_to_mail').on('change', function() {
var id = $(this).attr("id");
var value = $("#"+id).val();
$("#list_mail").append(value);
});
});
The concept is when user clicks on the select fields function will collect ids of fields and with these ids it will list emails in "#list_mail". 这个概念是当用户单击选择字段功能时,将收集字段ID,并使用这些ID在“ #list_mail”中列出电子邮件。
The question is; 问题是; when I click any option values all ids are writing side by side in the div.
当我单击任何选项值时,所有id并排写入div中。 How can I ignore previous selection and write only last selected id of every option on the div?
如何忽略上一个选择,只写div上每个选项的最后一个选择的ID? Considering "disciplineSelect", name="main", categoryID" and "observedPhase[]".
考虑“ disciplineSelect”,name =“ main”,categoryID“和” observedPhase []“。
Thanks in advance 提前致谢
Edit: language 编辑:语言
To overwrite any previous value you should set the html()
of the element, not append()
a new string value. 要覆盖任何先前的值,您应该设置元素的
html()
,而不是append()
为新的字符串值。 Try this: 尝试这个:
$('.list_who_to_mail').on('change', function() {
$("#list_mail").html($(this).val());
});
Note that you can also shorten your code as this
is a reference to the select
that changed. 请注意,您也可以缩短代码,因为
this
是对已更改select
的引用。 You don't need to select it again by its id
. 您无需通过其
id
再次选择它。
Update 更新
If you want to get the selected value from each option, you can use map()
: 如果要从每个选项中获取选定的值,可以使用
map()
:
$('.list_who_to_mail').on('change', function() {
var values = $('.list_who_to_mail').map(function() {
return $(this).val();
}).get();
$("#list_mail").html(values.join(','));
});
Don't use .append() , use .html() or .text() 不要使用.append()、. html()或.text()
$(function () {
$('.list_who_to_mail').on('change', function () {
var value = $(this).val();
$("#list_mail").html(value);
});
});
Also in your change handler, this
refers to the currently changed element so this
can be used to get the jQuery wrapper for the changed element, no need to use the id-selector
另外,在您更改处理,
this
指的是目前改变元素使this
可以被用来获取jQuery的包装器改变的元素,无需使用id-selector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.