问题
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