0

I have dynamicly added html elements(selectlists) in a form :

//Dynamicly adding selectlist elements
function newshit() {
    i = i + 1
    $("#mytable").append("<tr><td><div id='div" + i + "'><select id='elem" + i + "' name='elem" + i + "' class='ted'></select><input type='button' value='-' id='buttonminus" + i + "' style='width:5%;' onclick='removeelem(elem" + i + ",buttonminus" + i + "," + i + ")'/></div></td></tr>")
    getitems("elem" + i)
}

//filling lists
function getitems(item) {
    $.getJSON('@Url.Content("~/Stok/Items/")', {}, function (data) {
        $.each(data, function (i, c) {
            $("#" + item).append("<option value='" + c.Value + "' title='" + c.Text + "' label='" + c.Text + "'>" + c.Text + "</option>")
        })
    })
}

//removing element, when button next to it used
function removeelem(elem,buttonminus,i) {
    if ($("select").length > 1) {
        $("#div" + i).closest('tr').empty().remove()
    } else if ($("select").length <= 1) {
        alert("At least 1 of items must be chosen to create a group!")
    }
}

//checking elements and values existence
function check() {
    var slcts = $("select").serialize();
    alert(slcts)
}

im trying to get the value of each selectlist's selected option value and put them into an array than send them to my controller on form submit.

How can i achive this?

2
  • Why do this client side, why not just deal with it on the server side - these values will all be posted. Commented Oct 5, 2011 at 12:42
  • show me an example about it then :D Commented Oct 5, 2011 at 14:13

2 Answers 2

2

Need to check this, but I think that the following should work:

  1. Change your code so that the format of your ids is something like:

    id="elem[0]"

Then if your controller has a signature something like this:

public ActionResult Something(IEnumerable<string> elem)
{

}

Then his should "just work".

Sign up to request clarification or add additional context in comments.

5 Comments

what should i do to create element with id="elem[0]" this way? by string works or creating an objectArray?.
Exactly the same way that you are currently doing it, just need to change your string slightly.
are u sure that is working? cause i ve some trouble with it. can u show a sample of both view and controller parts.
No, don't have a easy way to check to hand. If you look in the posted formcollection, what do you have?
i made it at last yep ur code works just need to use a model between and <select name="elem[' + i + ']"></select> should start with i = 0; cause IEnumerable starts with 0 so its solved! Thanks.
1

You could use something like -

var selectdata = $("select").serialize()

This will return a string in the form <select name attribute1>=<chosen value1>&<select name attribute2>=<chosen value2> etc. You'd need to add a 'name' attribute to your select HTML when you create it for this to work.

Demo - http://jsfiddle.net/ipr101/fZXha/

You could then put the selectdata variable in a hidden field before the form was posted or send it via AJAX using the ajax or post methods.

3 Comments

Why do you want to send an array? Do you want to access an array in your controller?
reason that i wanna send it as array to know how many items dynamicly created
It is surely relatively trivial to split this string on '&' to get an array of name/values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.