开发者

How do I exclude certain form fields upon submission of the form without disabling the field

开发者 https://www.devze.com 2023-02-08 04:44 出处:网络
If I have a form with the following 3 fields: First Name Last开发者_Python百科 Name Date Of Birth

If I have a form with the following 3 fields:

First Name Last开发者_Python百科 Name Date Of Birth

When the user fills in all 3 fields and submits you will get the URL

http: //fakeURL.php?firstName=Fred&lastName=Flintstone&DateOfBirth=2/5/1952

However, if the user only fills in First Name and Date Of Birth you will get

http: //fakeURL.php?firstName=Fred&lastName=&DateOfBirth=2/5/1952 (where lastName has no value)

How do I achieve

http: //fakeURL.php?firstName=Fred&DateOfBirth=2/5/1952 (where lastName and value are removed from the URL)

I don't want to disable the input field upon using onsubmit. Is there a better way than disabled the input field?

Please help...


Just remove the "name" attribute from the input element.

// using jQuery
$('#inputId').removeAttr('name');

// native Javascript
document.getElementById('inputId').removeAttribute('name');


You must either:

  1. Remove or disable the field from the form before submitting it
  2. Don't submit the form, instead redirect to a URL you construct from the form yourself
  3. Make an AJAX request instead of leaving the page

Aside from those options, you can't submit this form via GET without all the inputs becoming part of the URL. That's part of the HTML and HTTP specifications.


if you are working on angular and using (ngModel), Remove the name attribute in the input field and add

[ngModelOptions]="{standalone: true}"


You should use forms with the GET method only when the new page is supposed to be bookmarked and passed around.

Since you are talking about you taking input from the user (and I presume you also store that input in a database or some similar permanent storage), you should be using POST instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消