How to create a delimited string dynamically with Jquery?
I'm creating a dynamic form builder where table rows can be dragged, dropped, reordered and so on. Most of this is working but when it comes to deleting rows how would I created an array or set of values that I could post back to the php script that I am using to update the database.
The way I am going to handle it is create a delimited string and post this back.
How would I go about creating the the string dynamically by appending a value like the following: 23| every time the user clicks a button? For example the if user click the button 3 times the sting would be 23|25|26| and then when they clicked save that value could be posted back to be processed.
This is the code I have for the delete function so far but its only removing the table rows and not actually generating the string.
$(".reMove"开发者_如何学C).live('click', function(e) {
$(this).parent().parent().remove();
var al = $(this).attr('rel');
$("#form1").find(".del").val(al);
e.preventDefault();
sortt();
});
I would use an array:
var deletions = [];
// When deleting
deletions.push(value);
// When sending to your PHP script, create a string via Array#join
var deletionsString = deletions.join("|");
Alternately, if you like, you can just use a string and append to it:
var deletions = "";
// When deleting
deletions += "|" + value;
// When sending to your PHP script
if (deletions.length > 0) {
deletions = deletions.substring(1); // Skip the leading "|"
}
...but I prefer the array route.
I can see you've got a form here. You could dynamically create a hidden input:
var $input = $('<input/>')
.attr('type', 'hidden')
.attr.('name', 'del[]')
.val(al);
$("#form1").append($input);
After posing this form back you could simply delete all records from $_POST['del'] array. Maybe this is the solution you're looking for?
精彩评论