0

I have preloaded hidden fields on the web form. these hidden fields are sent successfully to the controller when the jquery button is clicked

<input type ="hidden" class="serviceslist " value="1"/>
<input type ="hidden" class="serviceslist " value="2"/>

i am adding additional hidden fields by clicking the addmore button. these hidden fields are not be send to the controller when the jquery button is clicked . what might be the problem? are they not present in the dom?

<input type ="hidden" class="serviceslist " value="3"/>
<input type ="hidden" class="serviceslist " value="4"/>


  $('#addmoreservices').on('click', function () {
        var url = "/Quotation/AddmoreServices/";

        var serviceslist = $(".serviceslist ").serialize();        
        $.ajax({
            url: url,
            data: { serviceslist: serviceslist },
            cache: false,
            contenttype: 'application/json',
            dataType: "json",              
            type: "GET",
            success: function (data) {           

                return false;
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }

this is the code of the button i am trying to send whole bunch of hidden fields(preloaded and created) to the controller. the preloaded are being sent and created ones are left behind)

3
  • serialize only process input with a name Commented Feb 28, 2014 at 6:03
  • Can you add the code to add the input fields dynamically?? Or is it so that you add input fields in this function's success callback?? Commented Feb 28, 2014 at 9:12
  • The solution would be to make the 'click' handler of Commented Feb 28, 2014 at 9:30

4 Answers 4

1

You are adding the element dynamically so it is not registered when you set the click handler. Therefor also call the event handler once you have created the dynamic element

You have not shown enough code , therefor difficult to tell the exact solution,

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

1 Comment

change the first line of where you attached event handler with $(document.body).on('click','#addmoreservices' , function () { , this will attach event handler for dynamic element also
0

Add unique names to the fields. Don't use the same name over and over again.

Comments

0

if your input has a name,the serialize() can process

<input type ="hidden" class="serviceslist " value="1" name="a" />

but if all your input name is "a",the serialize result is

a=1&a=2&a=3....

i think that is not what you want
maybe you want

1,2,3,4....

try use

$(".serviceslist").map(function(){return $(this).val();}).get().join(",")

Comments

0

Try to make the 'click' event handler a separate js function like

$('#addmoreservices').on('click', addHandler)

var addHandler = function(){...};

Then on the addmore button click handler, after you add the new input to dom, again bind the addmoreservices button click handler 'addHandler'. This will include the newly created inputs in the 'addHandler' function.

Comments

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.