我的html代码中有2个<buttons>
,其中一个有onclick
函数,但另一个只是我尚未定义的按钮。奇怪的是,未定义的按钮与另一个具有相同的onclick
属性。有人可以解释为什么会这样吗?
以下是代码段:
saveRuleSelections: function() {
let blackjackRules = [];
let rules = document.getElementsByClassName("rule");
for (let i = 0; i < rules.length; i++) {
blackjackRules.push(rules[i].value);
}
let jsonBlackjackRules = JSON.stringify(blackjackRules);
localStorage.setItem("blackjackRules", jsonBlackjackRules);
document.getElementById("rules_overlay").style.display = "none";
}
<form>
<p>Surrender Allowed:
<select class="rule">
<option value="early">Early</option>
<option value="late">Late</option>
<option value="no" selected>No</option>
</select>
</p>
<button id="select_rules_button" onclick="trueCountApp.saveRuleSelections()">Select Rules</button>
<button>Restore Default</button>
</form>
答案 0 :(得分:1)
两个表单都具有相同的提交值,因为它们在表单内部没有任何不同的值,现在两者都将采用相同的提交表单的值,因此触发该功能,它就像一个克隆。尝试
<form>
<p>Surrender Allowed:
<select class="rule">
<option value="early">Early</option>
<option value="late">Late</option>
<option value="no" selected>No</option>
</select>
</p>
<button id="select_rules_button" onclick="trueCountApp.saveRuleSelections()">Select Rules</button>
<button type="reset" value="Reset">Restore Default</button>
</form>
答案 1 :(得分:0)
可能是您的第二个按钮只是执行默认操作吗?默认操作取决于按钮的类型,默认提交(IE中除外)。
what's the standard behavior when < button > tag click? will it submit the form?