gpt4 book ai didi

javascript - 为什么 onsubmit 不起作用,如果我在控制台中调用它运行的函数?

转载 作者:行者123 更新时间:2023-11-30 14:18:14 26 4
gpt4 key购买 nike

我有这个 html:

<!doctype html>
<html class="no-js" lang="">

<head>
<meta charset="utf-8">

<script src="js/filename.js"></script>
</head>

<body>
<form id = "dp5" action="" method="POST" onsubmit="Write_Text()">
<h3>5- Do you know any browsers?</h3>
<input id = "No" type="radio" name="dp5N" value="false">
<label for = "No">No </label>

<input id = "yes" type="radio" name="dp5S" value="true">
<label for = "yes">Yes</label>

<label for = "text">6 - Which?</label>
<input id = "text" type="text" name="dp5text" value="">
</form>

<div id="next">
<input id="sub" type="submit" name="submit" value="Next">
</div>
</body>
</html>

还有这个 javascript“文件名”:

function Write_Text() {
let x = document.forms["dp5"]["No"].value;

if (x === "false") {
document.getElementById("text").disabled=true;
document.getElementById("text").value="";
} else {
document.getElementById("text").disabled =false;
}

文本框应该开始禁用,只有当用户选择"is"选项时才可用。该功能根本不起作用。

最佳答案

您的提交按钮在表单之外。将它放在表单中,它将起作用。

<form id = "dp5" action=""  method="POST" onsubmit="Write_Text(); return false">
<h3>5- Do you know any browsers?</h3>
<input id = "No" type="radio" name="dp5N" value="false">
<label for = "No">No </label>

<input id = "yes" type="radio" name="dp5S" value="true">
<label for = "yes">Yes</label>

<label for = "text">6 - Which?</label>
<input id = "text" type="text" name="dp5text" value="">

<div id="next">
<input id="sub" type="submit" name="submit" value="Next">
</div>
</form>

关于javascript - 为什么 onsubmit 不起作用,如果我在控制台中调用它运行的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com