在前端开发中,可以通过以下几种方式从另一个选择中获取上一个选择的值到当前选择的onclick事件中:
var previousValue;
function onChangeHandler() {
previousValue = document.getElementById("previousSelect").value;
}
function onClickHandler() {
var currentValue = document.getElementById("currentSelect").value;
console.log("Previous value: " + previousValue);
console.log("Current value: " + currentValue);
}
function onChangeHandler() {
var previousValue = document.getElementById("previousSelect").value;
document.getElementById("currentSelect").setAttribute("data-previous-value", previousValue);
}
function onClickHandler() {
var currentValue = document.getElementById("currentSelect").value;
var previousValue = document.getElementById("currentSelect").getAttribute("data-previous-value");
console.log("Previous value: " + previousValue);
console.log("Current value: " + currentValue);
}
function createOnChangeHandler() {
var previousValue;
return function() {
previousValue = document.getElementById("previousSelect").value;
};
}
var onChangeHandler = createOnChangeHandler();
function onClickHandler() {
var currentValue = document.getElementById("currentSelect").value;
console.log("Previous value: " + onChangeHandler.previousValue);
console.log("Current value: " + currentValue);
}
这些方法可以根据具体的需求选择使用,根据前端框架和开发环境的不同,可能会有其他更适合的方式。
领取专属 10元无门槛券
手把手带您无忧上云