javascript - 使用 form.submit 时必需的属性不起作用

标签 javascript html

我正在使用 html5 必需属性来检查用户是否填写了必填字段。只要我使用提交按钮,一切似乎都运行良好。但是,当我将提交按钮更改为普通按钮,然后使用 onClick 事件调用 JavaScript 函数,以便在 javascript 中完成一些验证,然后我使用 document.form.formname.submit() 提交表单。但是当我这样做时,不会检查必填字段是否为空。我正在使用 Chrome 11.0 这是 html 代码:

<form method="post" action="./post_discussion.php" name="newdiscussion">
<span style="font-size:16px; font-weight:bold; font-family:Arial, Helvetica, sans-serif">Title: </span>

<input id="title_text" name="title_text" style="width:650px; margin-left:10px;" type="text" placeholder="Whats the title of your discussion? Be clear." required="required" />
<br/><br/>
<div style="margin-top:10px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;">Make your point</div>

<textarea id="text_area" name="text_area" style=" height:200px; margin-top:10px;" placeholder="Describe your point of discussion. Be clear on the point and provide convincing
 evidence.A claim without evidance will not be encouraged by the site." cols="90" required="required"/>
</textarea><br />

<div style="margin-top:10px; margin-bottom:5px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;">Tags</div>
<input id="tags_textfield" onkeyup="tags_generator()" name="tags_textfield" type="text" style="width:500px;" placeholder="Enter atleast one tag" onblur="clear_tags()" autocomplete="off" required="required"/>

<ul id="tags_ul"><div id="tag_drop_down"></div></ul><br/>

<div style="margin-top:10px; margin-bottom:5px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;">Users involved</div>
<input id="uids_textfield" name="uids_textfield" type="text" style="width:500px;" placeholder="Enter all the users involved" required="required"/>
<br/>

<?php
 if(!isset($_COOKIE["username"]))
 {
     echo '<div id="comment">You are not signed in. Please enter your credintials to post the discussion.</div><br/>';
    echo 'Username: <input id="bottom_username" type="text" name="username"/><br/> 
       Password: <input id="bottom_password" name="password" type="password"/>';
    echo   '<br/><br/>
<input id="post_button" onclick="new_discussion(0)" type="button" value="Post the discussion" style="margin-top:10px;"/>';
 }
 else
 echo   '<br/><br/>
<input id="post_button" type="button" onclick="new_discussion(1)" value="Post the discussion" style="margin-top:10px;"/>';
?>

这是javascript:

function new_discussion(arg)
{
    if(arg==1)
    {

        document.forms.newdiscussion.submit();
    }
    else
    {
        //some logic here
    }
}

最佳答案

还要确保您在支持“必需”的浏览器上测试它:http://www.w3schools.com/tags/att_input_required.asp

The required attribute is supported in Internet Explorer 10, Firefox, Opera, and Chrome.

Note: The required attribute of the tag is not supported in Internet Explorer 9 and earlier versions, or in Safari.

关于javascript - 使用 form.submit 时必需的属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6150620/

相关文章:

php - 缩小移动设备上的网页版本

javascript - Knockout - 如何用新值重置动态可观察数组

javascript - 如何定义可在所有 API 中使用的基本 url

javascript - 表排序器中的链式一列和多列过滤器

jquery - 在表中显示多个值

html - 覆盖 html 表 td 样式

javascript - polymer 1.x : Imperatively adding event listener

javascript - javascript 中带有过滤器的元素计数器

javascript - 获取div中可用空间的高度和宽度

javascript - JQuery按钮点击不注册