admin管理员组文章数量:1023046
I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon submit?
<form action='...' method='post' id='mySubmitForm'>
<input type='text' name='name'>
<input type='text' name='email'>
<input type='text' name='phoneNumber'>
<input type='submit' value='Save'>
</form>
Is there a easy way to check with JavaScript if the form is empty and delete it before submission if so?
I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon submit?
<form action='...' method='post' id='mySubmitForm'>
<input type='text' name='name'>
<input type='text' name='email'>
<input type='text' name='phoneNumber'>
<input type='submit' value='Save'>
</form>
Is there a easy way to check with JavaScript if the form is empty and delete it before submission if so?
Share Improve this question asked Jun 23, 2011 at 18:24 hogni89hogni89 1,7196 gold badges22 silver badges41 bronze badges 1- what do you want specifically? do you want to disable the fields that are empty before the form submits? in most cases it makes more sense to figure out if a field is empty AFTER the form submission. with php/asp or what ever server side lang you are using to deal with the input. – Achshar Commented Jun 23, 2011 at 18:29
5 Answers
Reset to default 2There is a submit
event that the browser throws before form submission that you can use.
reference: http://www.quirksmode/js/forms.html
Return false
if you don't want the form to be submitted, true
if you want it to happen. In the event, delete / add the extra inputs that you want accordingly.
function validate(formName)
{
var form = document.forms[formName];
//validate, and do stuff
//remove items that you want with a call like this
form.removeChild(document.getElementById(id));
form.submit();
}
If this is for validation, you should really be doing validation server side, not client side.
You would call this function like so:
<input type=BUTTON onClick="validate('myForm')"/>
You can use jQuery, which is probably the easiest way.
$(document).ready(function() {
$('#.mySubmitForm').submit(function(event) {
event.preventDefault();
$('input[type=text]').each(function() {
var inputElement = $(this);
inputElement.val() == "" ? inputElement.remove() : null;
});
$(this).trigger('submit');
});
});
I didn't test that code, but it should delete the empty form values before submit, then remove them.
function onsubmit() {
[].forEach.call(document.querySelectorAll('#mySubmitForm input[type=text]'), function(col) {
if(col.value=='') col.disabled = 'disabled';
});
}
and onsubmit="onsubmit()"
in your <form>
tag
Checking through javaScript is easy, but I'd advise you to have-and-assign an id
attribute to your form
elements
You can check in the following way,
var email = document.getElementById('email').value;
and you can remove email
from your form
as shown below
form.removeChild(document.getElementById('email'));
form.submit();
you can have a look at Adding and Removing HTML elements dynamically with Javascript for more details.
I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon submit?
<form action='...' method='post' id='mySubmitForm'>
<input type='text' name='name'>
<input type='text' name='email'>
<input type='text' name='phoneNumber'>
<input type='submit' value='Save'>
</form>
Is there a easy way to check with JavaScript if the form is empty and delete it before submission if so?
I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon submit?
<form action='...' method='post' id='mySubmitForm'>
<input type='text' name='name'>
<input type='text' name='email'>
<input type='text' name='phoneNumber'>
<input type='submit' value='Save'>
</form>
Is there a easy way to check with JavaScript if the form is empty and delete it before submission if so?
Share Improve this question asked Jun 23, 2011 at 18:24 hogni89hogni89 1,7196 gold badges22 silver badges41 bronze badges 1- what do you want specifically? do you want to disable the fields that are empty before the form submits? in most cases it makes more sense to figure out if a field is empty AFTER the form submission. with php/asp or what ever server side lang you are using to deal with the input. – Achshar Commented Jun 23, 2011 at 18:29
5 Answers
Reset to default 2There is a submit
event that the browser throws before form submission that you can use.
reference: http://www.quirksmode/js/forms.html
Return false
if you don't want the form to be submitted, true
if you want it to happen. In the event, delete / add the extra inputs that you want accordingly.
function validate(formName)
{
var form = document.forms[formName];
//validate, and do stuff
//remove items that you want with a call like this
form.removeChild(document.getElementById(id));
form.submit();
}
If this is for validation, you should really be doing validation server side, not client side.
You would call this function like so:
<input type=BUTTON onClick="validate('myForm')"/>
You can use jQuery, which is probably the easiest way.
$(document).ready(function() {
$('#.mySubmitForm').submit(function(event) {
event.preventDefault();
$('input[type=text]').each(function() {
var inputElement = $(this);
inputElement.val() == "" ? inputElement.remove() : null;
});
$(this).trigger('submit');
});
});
I didn't test that code, but it should delete the empty form values before submit, then remove them.
function onsubmit() {
[].forEach.call(document.querySelectorAll('#mySubmitForm input[type=text]'), function(col) {
if(col.value=='') col.disabled = 'disabled';
});
}
and onsubmit="onsubmit()"
in your <form>
tag
Checking through javaScript is easy, but I'd advise you to have-and-assign an id
attribute to your form
elements
You can check in the following way,
var email = document.getElementById('email').value;
and you can remove email
from your form
as shown below
form.removeChild(document.getElementById('email'));
form.submit();
you can have a look at Adding and Removing HTML elements dynamically with Javascript for more details.
本文标签: javascriptDelete html element at submitStack Overflow
版权声明:本文标题:javascript - Delete html element at submit - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745522295a2154367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论