问题
I don't understand pretty well how the hidden fields arrays work, I have this input:
<input type="hidden" id="ftpIds[]" value=""/>
How can I add and remove values to and from that array in Jquery/Javascript? I have something like this:
$(document).ready(function() {
$('#button').click(function(){
var ids=$("#txtIds").val();
$("#ftpIds").addToArray(ids);
});
});
and to remove do I need something like this?
$("#ftpIds").removeFromArray('3');
I want to pass a list to a Controller in Grails, so I want to have like a list or array named ftpIds. Is it right? or Is this the better way to do this?
回答1:
As I said in my comment, this link will probably help you a lot: JSFiddle
After the submit, in your controller, the split() method will help you to convert your String to an array of String.
For example:
def myList = params.myInput.split(',')
And I think (not sure) that you need to add a name for you input, in order to use the params scope (like <input type="hidden" name="myInput" />
).
回答2:
If you array is ftpIds[] just write
delete ftpIds[3];
Keep in mind that delete does not alter the array.length() property.
You can also use array.pop() to remove the last element in the array and reduce size by one. It will also return the removed element.
But i don't see an array... I see an ID
来源:https://stackoverflow.com/questions/23832502/add-and-remove-values-in-a-hidden-field-array