简体   繁体   English

如何忽略上一个选择,只写上一个选择

[英]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());
});

Example fiddle 小提琴的例子

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(','));
});

Example fiddle 小提琴的例子

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.

相关问题 如何配置 jquery-ui datepicker 以仅在每个月的最后一个星期五启用选择? - How can I configure jquery-ui datepicker to enable for selection only the last friday of each month? 如何根据用户所做的选择将只读下拉列表/文本框更改为读/写? - How can I change read only dropdowns/text boxes to read/write depending on selection made by user? 只有所选图像的边框颜色必须更改。 当我单击 div 中的某个图像时,必须删除先前的图像选择 - Only the border color of selected image must change. And when I click on some image inside div previous image selection must removed 如何刷新选择了相同选择的页面? (玉/ HTML) - How can I refresh a page with the same selection selected? (Jade/HTML) 仅在选择时如何显示自动完成选择? - How to display a autocomplete selection only when selected? 如何保持下拉列表在刷新页面上的最后一次选择 - How Can I keep the dropdown last selected on refresh page 如何根据之前的选择进行选择? - How do I make a selection based on a previous selection? 如何从另一个选择中将先前选择的值转换为当前选择onclick? - How to get previous selected value from another selection into current selection onclick? 只能显示有关最后创建的对象的信息。 覆盖上一个 - Can only display information about the last created object | Overwrites previous 如何禁用html5 datepicker上的仅选定日期? - How can I disable only selected dates on html5 datepicker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM