How to get the value of a group of dynamically created text boxes?
I have a group of dynamically created (while loop) text boxes / input fields driven from a database:
<input type='text' value='".htmlentities($page['special'], ENT_QUOTES)."' />
<input type='submit' class='button' value='Edit' />
&l开发者_运维知识库t;input type='submit' class='button' value='Delete' />
The number of fields may change upon the number of records in the database. At the right of every field there are two buttons: Edit and Delete. These two buttons (via Jquery $get
) are used to update or delete the dB records.
My problem is that I don't know how to 'link' / bind the buttons to their field. I guess I have to use Jquery index()
or eq()
methods, but I don't know how nor if it is correct.
Here's an example of when the edit or delete button is clicked. You can find the closest text box traversing up the DOM.
$('.button').click(function(){
var text_box = $(this).closest('input[type="text"]'); // get closest text box to the edit and delete button.
});
This assumes your HTML structure is consistent to the one you posted above.
Well if your inputs had a class, you could go through them like this:
$(".yourinputs").each({
$(this).next().click(/*edit handler*/);
$(this).next().next().click(/*delete handler*/);
});
Of course this assumes an HTML structure, so it might be wiser to give each of your inputs an ID, in a data field, like <input data-id="21">
, and then in your edit button a real ID, like edit-21
, and then you can fetch them realiably in the loop by doing $("edit-" + $(this).data("id"))
.
Give the input an id='recordEdit_".$page['YourRecordSetID']."'
then you can use that id to get which recordset it links to
精彩评论