我正在使用html5必需屬性來檢查用戶是否填寫了必填字段。只要我使用提交按鈕,每件事似乎都可以正常工作。但是,當我將提交按鈕更改爲普通按鈕,然後使用onClick事件調用JavaScript函數,以便在JavaScript中完成一些驗證後,我使用document.form.formname.submit()提交表單。但是,當我這樣做時,所需的字段沒有被檢查,無論它們是否爲空。我使用Chrome 11.0 下面是HTML代碼:當使用form.submit時,必需屬性不起作用
<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
}
}
你爲什麼要一個正常的按鈕,提交按鈕?一個正常的按鈕不與表單鏈接,瀏覽器不檢查輸入字段的必需屬性! – ChristianB 2011-05-27 10:53:22
@ChristianB你可以看到,當沒有設置cookie時,我要求輸入用戶憑據,並且想要使用ajax驗證提交的憑證(此處未顯示)。其他明智的形式正常提交。 – sasidhar 2011-05-27 17:51:45