简体   繁体   English

满足条件时从多个下拉列表中进行选择更新

[英]Make selections from multiple drop-downs update when criteria is met

I feel like this question is very vague, and I apologize because I can't seem to come up with a better name.我觉得这个问题很模糊,我很抱歉,因为我似乎想不出更好的名字。

Ahem.咳咳。 As for the actual question.至于实际问题。 I've got two drop-down menus, and I'd like to make it so that whenever I have the correct value in each one, ex.我有两个下拉菜单,我想这样做,以便每当我在每个下拉菜单中都有正确的值时,例如。 Level 1 and Barbarian , it updates the connected fields. Level 1Barbarian ,它更新连接的字段。 This works just fine with what I have here, but the problem is, if I leave Barbarian how it is and change the level to Level 2 , it won't update the connected fields until Barbarian has been switched to something else and then put back on Barbarian .这对我这里的东西很好用,但问题是,如果我让Barbarian并将级别更改为Level 2 ,它不会更新连接的字段,直到Barbarian切换到其他东西然后放回去关于Barbarian How can I make it update right when the fields are the correct for a certain setup?当字段对于特定设置正确时,如何使其正确更新? I'm assuming some sort of listener, but I really don't know all that much about Javascript, as I'm new to it all.我假设某种听众,但我真的不太了解 Javascript,因为我是新手。

function classDefine(){
var playerClass = document.getElementById('class').value;
var playerLevel = document.getElementById('level').value;
if (playerClass == "Barbarian" && playerLevel == "Level 1") {
document.getElementById("fort").value = 2;
document.getElementById("ref").value = 2;
document.getElementById("will").value = 2;
document.getElementById("baseAttackBonus").value = 2;
}
else if (playerClass == "Barbarian" && playerLevel == "Level 2"){
document.getElementById("fort").value = 5;
document.getElementById("ref").value = 5;
document.getElementById("will").value = 5;
document.getElementById("baseAttackBonus").value = 5;
}
}

You can use the onChange listener on each of the select boxes to run the function whenever either of them changes.您可以在每个选择框上使用 onChange 侦听器以在其中任何一个发生更改时运行该函数。

<select id="class" onChange="classDefine()">
    <option>...</option>
</select>

or in javascript或在 javascript 中

document.getElementById('class').addEventListener("change", classDefine);

You can use the change event handler that gets activated whenever a new selection is made in your dropdown.您可以使用在下拉列表中进行新选择时激活的更改事件处理程序。 You can add one for each of your dropdown and then put your logic for addressing the corresponding matches between the two.您可以为每个下拉列表添加一个,然后放置用于解决两者之间相应匹配的逻辑。 Below is an example of how you would attach an event handler to one of them.下面是如何将事件处理程序附加到其中之一的示例。

document.getElementById('class').addEventListener("change", function (event) {
     //your logic
     alert("something new was selected"); 
});

How this helps.这有什么帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM