Reputation: 4385
I have an asp.net web form that gathers details about clinics and patients. On the bottom part of the form i have a block that basically asks for patient information (firstname,lastname, address etc) I can have n number of patients (n can range from 1 to 10) . I was thinking i would write one html block and ask a question like
Add another patient?
where i would have to dynamically generate a similar block like the first one with unique id's .
Whats the best approach to this type of problem? How do i deal with validation?
Thanks
Upvotes: 0
Views: 207
Reputation: 348
I advise you to use multiple HTML inputs, like:
<input type="text" name="name[]"/>
Then, in your JavaScript you just append, or clone like the other guy said, the fields! Than you can easily validate in your server side, because every field in your form will be an array, like
(name[0] = 'patient1', name[1] = 'patient2', etc)
.
Count the number of inputs, and then validate they.
Upvotes: 0
Reputation: 2557
use the $.clone
method
here is an example
make sure you don't use ID though , cuz you will end up with duplicate ids
i see that you dont really understand the concept in here . ID's are ment to be unique (think of it as a government ID number ) not two can have the same ID.
while classes are not unique (just like names , many people share the same name ) , they can be assigned to more than one element .
use id's for elements that should never be repeated on the same page .
for example the main header can have an id of id="header_container"
. While the items of a list can never have the same id because they are not unique (notice that we are talking about the tag not it's content).
for id's the header should be ok . so :
<div id="header">... header contents ...</div>
and for classes a list of items should be ok too
<ul>
<li class="someone" > me <li>
<li class="someone" > you <li>
<li class="someone" > he <li>
</ul>
you CAN NOT do :
<ul>
<li id="someone" > me <li>
<li id="someone" > you <li>
<li id="someone" > he <li>
</ul>
you can use any type of valid css selector in jquery
so if i were to get the header i will use
$('#header')
where
#
is the sign for id
and if i were to select ALL the list items
$('.someone')
where
.
is the sign for id
first of all you should know that the use of id's is not a really smart idea . i rarely use them (if you can avoid the trouble then do ) .
so use classes . because they can be still only assigned to one element if you do so .
or use the new data
attribute .
but if you are 100% sure that they will never repeat then use them . example of that is a set of records in the database . sure thing they dont have the same id then just use the attribute id for the elements.
well simply assign a class . if you dont want to then simply
$('input')
which will select all the input tags , still a bit inconvenient though , but no worries. you can select an input by it's name via
$('input[name="the_name_of_the_input"]')
please read more about the topic by googling it . i just append to have a free time . so i invested it . but work hard to get the answer thats how it sticks
Upvotes: 2