gpt4 book ai didi

javascript - onclick 事件在表单结束标记后不起作用

转载 作者:行者123 更新时间:2023-12-03 11:15:45 28 4
gpt4 key购买 nike

问题是 onclick 事件按钮在表单开始标记之前工作正常(在 html 文档的开头)但它在表单关闭标签后不起作用!我想知道为什么会这样?我按钮的位置和js函数调用有什么关系?

    <form name="Register" action="#" method="post">
<fieldset>
<legend>Personal Information</legend>
<label>Username</label>
<input type="text" name="username" placeholder="Enter your name">

<br><br>

<label>Password</label>
<input type="password" name="password" placeholder="Enter your password">

<br><br>
<label>Message</label>
<br>
<textarea name="message" rows="8" cols="50" readonly>Your Message (currently disabled)</textarea>
<br><br>

<label>Upload your cv</label>
<input type="file" name="cv">

<br><br>

<label>Remember me</label>
<input type="checkbox" name="remember">
</fieldset>

<br><br>

<fieldset>
<legend>Favourits</legend>
<label>Choose your browser</label>
<br>
<input type="radio" name="browser"> Oprera
<br>
<input type="radio" name="browser"> Chrome
<br>
<input type="radio" name="browser"> Firefox

<br><br>

<label>Choose your Mobile</label>
<br>
<input type="radio" name="mobile"> Samsung
<br>
<input type="radio" name="mobile"> Apple
<br>
<input type="radio" name="mobile"> Nokia
</fieldset>
<br><br>

<fieldset>
<legend>Button Options</legend>
<input type="hidden" value="testing">
<input type="submit" name="submit_Register" value="submit">
<input type="reset" name="" value="reset">
</fieldset
</form>

<input type="button" name="sayHello" value="sayHello" onclick="sayHello()" />

这是脚本

<script>
function sayHello()
{
alert("Hello");
}
</script>

最佳答案

您的按钮因无效关闭而无法使用 </fieldset>标签:

</fieldset

function sayHello()
{
alert("Hello");
}
<input type="button" name="sayHello" value="sayHello" onclick="sayHello()" />
<form name="Register" action="#" method="post">
<fieldset>
<legend>Personal Information</legend>
<label>Username</label>
<input type="text" name="username" placeholder="Enter your name">

<br><br>

<label>Password</label>
<input type="password" name="password" placeholder="Enter your password">

<br><br>
<label>Message</label>
<br>
<textarea name="message" rows="8" cols="50" readonly>Your Message (currently disabled)</textarea>
<br><br>

<label>Upload your cv</label>
<input type="file" name="cv">

<br><br>

<label>Remember me</label>
<input type="checkbox" name="remember">
</fieldset>

<br><br>

<fieldset>
<legend>Favourits</legend>
<label>Choose your browser</label>
<br>
<input type="radio" name="browser"> Oprera
<br>
<input type="radio" name="browser"> Chrome
<br>
<input type="radio" name="browser"> Firefox

<br><br>

<label>Choose your Mobile</label>
<br>
<input type="radio" name="mobile"> Samsung
<br>
<input type="radio" name="mobile"> Apple
<br>
<input type="radio" name="mobile"> Nokia
</fieldset>
<br><br>

<fieldset>
<legend>Button Options</legend>
<input type="hidden" value="testing">
<input type="submit" name="submit_Register" value="submit">
<input type="reset" name="" value="reset">
</fieldset>
</form>
<input type="button" name="sayHello" value="sayHello" onclick="sayHello()" />

关于javascript - onclick 事件在表单结束标记后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60240692/

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