HTML form required command not working?

最后都变了- 提交于 2019-12-11 19:44:12

问题


I'm trying to get my form to require certain fields be filled out before the form can be submitted.

I searched and found that the command is supposed to be this simple command

<input type="text" name="name" id="name" placeholder="Name" required />

But for some reason on my form it submits it even when specified fields are not completed.

I even tried creating a brand new file in Dreamweaver with a new form with just a required input item and submit button and tried it in different browsers and it didn't work in Chrome or Safari. I copied it exactly from a YouTube video I found here with no luck http://www.youtube.com/watch?v=M73FroYgkt0

Here is my site so you can examine the code. The form is at the bottom.

http://www.YourFlyersDelivered.com


回答1:


The problem with your form isn't your html, it is your javascript.

On line 33 of config.js, you have the following line:

jQuery('form .form-button-submit').click(function(e) { e.preventDefault(); jQuery(this).closest('form').submit(); });

This line is preventing the default action of your submit button, bypassing the required attribute on your input element, and submitting the form. If you remove this line, I'm sure it will work.. Examples:

Your current method of form submission: http://jsfiddle.net/Daedalus/2FY9g/ <- does not work

Without the default action prevented: http://jsfiddle.net/Daedalus/2FY9g/1/ <- works




回答2:


i would use js to validate the entries a simple validate.js file that contains something like:

function validate username(){

var user = document.getElementsByName("username")[0];

if(user.value == "") return false; }

sorry about the formatting.. im new to this forum and still getting used to it :)




回答3:


Try this, add runat="server" to form......

<form id="form1" runat="server">

<input id="name" required />

<input type="submit" value="Search" />

</form>


来源:https://stackoverflow.com/questions/18625869/html-form-required-command-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!