addEventListener duplicating Select Options Javascript

放肆的年华 提交于 2019-12-12 02:35:09

问题


I created a javascript function to copy data from an input on another jQuery tab to a Summary tab. I am having an issue with duplication with a dropdown.

I need for it to replace the exisiting information when choosing another selection if a change is needed.

HTML - Dropdown

<select id="accountCoordinator1" class="txtbxList">
  <option>Select</option>
  <option>Jon</option>
  <option>Lori</option>
</select>



HTML - Placement

<table>
    <tr>
        <td id="summaryAccountCoordinator1"></td>
    </tr>
</table>



JavaScript

(function(){

$(document).ready(function () {
    if (window.addEventListener) {
        document.getElementById("accountCoordinator1").addEventListener("change", accountCoordinator1GV, false);

    } else if (window.attachEvent) {
        document.getElementById("accountCoordinator1").attachEvent("change", accountCoordinator1GV);
    }
});

var accountCoordinator1GV = function () {
    var accountCoordinator1GV = $("#accountCoordinator1").val();
    var ac1DIV = $("<h3>Account Coordinator</h3>" + "<div class='summaryDIV'>" + accountCoordinator1GV + "</div>");
    $("#summaryAccountCoordinator1").append(ac1DIV);
};

});

回答1:


If you want to replace the existing information, use .html() instead of .append(). It might be easier to just change the text of .summaryDIV:

$(document).ready(function() {
    $("#accountCoordinator1").change(function() {
        $("#summaryAccountCoordinator1 .summaryDIV").text($(this).val());
    });
});


来源:https://stackoverflow.com/questions/17182249/addeventlistener-duplicating-select-options-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!