admin管理员组文章数量:1337125
My alert message pop ups before the loading/submission of a record, so if the user didn't input any data. It still pops up. What additional javascript function should I use for my alert box?
Here's my code
function myFunction() {
alert("Adding Succesful!");
}
<label for="bName" class="control-label col-xs-4"><p class="left">Brand Name</p></label>
<input name="bName" class="form-control req" required/>
<button type="submit" class="btn btn-default bt" onclick="myFunction()" style="align:right;">ADD</button>
My alert message pop ups before the loading/submission of a record, so if the user didn't input any data. It still pops up. What additional javascript function should I use for my alert box?
Here's my code
function myFunction() {
alert("Adding Succesful!");
}
<label for="bName" class="control-label col-xs-4"><p class="left">Brand Name</p></label>
<input name="bName" class="form-control req" required/>
<button type="submit" class="btn btn-default bt" onclick="myFunction()" style="align:right;">ADD</button>
Share
Improve this question
edited Oct 18, 2015 at 13:50
MinusFour
14.4k4 gold badges31 silver badges39 bronze badges
asked Oct 18, 2015 at 13:37
VistaVista
2012 gold badges5 silver badges27 bronze badges
8
- If you want to display the alert after something happens then you'd need to write the code in response to that event. If the form is being submitted via AJAX then you'd display the alert in the response handler for the AJAX call. If the form is being submitted at the browser level then you'd need to display the alert on the next page instead of this one. – David Commented Oct 18, 2015 at 13:40
-
Do you mean you need to stop submission of the form when form validation fails ? if so, add
return false;
to yourmyFunction
when your form validation fails. – Sachin Commented Oct 18, 2015 at 13:41 - or atleast the "required/" must first shows up before the alert – Vista Commented Oct 18, 2015 at 13:45
- @Sachin If there is any false validation the alert message will not pop up – Vista Commented Oct 18, 2015 at 13:48
- You have only one input field? – Sandeep Nayak Commented Oct 18, 2015 at 13:52
2 Answers
Reset to default 3Use HTML5 validity checks to see if the form is valid or not.
var form = document.getElementById('f');
function myFunction() {
if (form.checkValidity()) {
alert("Adding Succesful!");
}
}
<form id="f">
<label for="bName" class="control-label col-xs-4">
<p class="left">Brand Name</p>
</label>
<input name="bName" class="form-control req" required/>
<button type="submit" class="btn btn-default bt" onclick="myFunction()" style="align:right;">ADD</button>
</form>
Try using:
<label for="bName" class="control-label col-xs-4"><p class="left">Brand Name</p></label>
<input name="bName" class="form-control req" required/>
<button type="submit" class="btn btn-default bt" onclick="return myFunction(this)" style="align:right;">ADD</button>
(note I changed the onclick to include a return
and a this
)
And use it with the JavaScript:
function myFunction(t)
{
if($(t).prev().val())
{
alert('Adding Succesful!')
return true;
}else{
return false;
}
}
本文标签: javascriptAlert message will show up after the submissionStack Overflow
版权声明:本文标题:javascript - Alert message will show up after the submission - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742393643a2466482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论