简体   繁体   中英

Add and Remove values in a hidden field array

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?

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" /> ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM