<form action="welcome.php" method="post" onsubmit="return validate();">
<fieldset>
<legend> <b>Personal Info</b> </legend>
<pre>
<b>First Name</b> <input type="text" id="fname" size="30">
<b>Last Name</b> <input type="text" id="lname" size="30">
<b>Phone Number</b> <input type="number" id="fn" size="30">
    <fieldset>
    <legend> <b>Gender</b> </legend>
    <input type="radio" id="male" name="gender" value="Male">Male
    <input type="radio" id="female" name="gender" value="Female">Female
    </fieldset>
</pre>
</fieldset>
<input type="button" value="submit">
</form>

JavaScript函数是validate()。如果“名字”或“姓氏”或“性别”为空,则用户将看到一条消息,并且表单不会提交。通过使用javascript函数将有什么解决方案。

最佳答案

通过使用jQuery,您可以获得所有输入值

例:

var fname= $('#fname').val();
var lname= $('#lname').val();

将必填属性添加到输入字段进行验证
<form action="welcome.php" method="post" onsubmit="return validate();">
<fieldset>
<legend> <b>Personal Info</b> </legend>
<pre>
<b>First Name</b> <input type="text" id="fname" required size="30">
<b>Last Name</b> <input type="text" id="lname" required size="30">
<b>Phone Number</b> <input type="number" id="fn" required size="30">
    <fieldset>
    <legend> <b>Gender</b> </legend>
    <input type="radio" id="male" name="gender" value="Male">Male
    <input type="radio" id="female" name="gender" value="Female">Female
    </fieldset>
</pre>
</fieldset>
<input type="button" value="submit">
</form>

10-06 01:04