4

I wanted to get the input field which is in div using jquery .but I am unable to to get it. I want to iterate it with class model and add into the array of string and pass it to controller.

below is my one div that have class model i will make multiple div like this and iterate it to its to get the input field in it and pass it to my controller

   <div class="models">
            <div class="row" >

                <div class="col-md-12" >
                    <label class="control-label" >NIC</label>
                    <input type="text" class="form-control" name="VNIC" id="VNIC" required>
                </div>
            </div>



            <div class="row">
                <div class="col-md-12">
                    <label class="control-label">First Name</label>
                    <input type="text" class="form-control" name="VfirstName" id="VfirstName" required>
                </div>
            </div>



<script type="text/javascript">
                $("#Visitor-form").on("submit", function () {
                    //Code: Action (like ajax...)
                    var slides = $(".models");
                    console.log(slides);
                    for (i = 0; i < slides.length; i++) {

                        var previsitor = [
                            { NIC: slides[0].value, V_Name: slides[0].value }
                        ];
                    }
                    previsitors = JSON.stringify({ 'previsitors': previsitor });
                    console.log(previsitors);
                    $.ajax({
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        type: 'POST',
                        url: '/PreVisitor/addMultipleVisitor',
                        data: previsitors,
                        success: function () {
                            $('#result').html('"Pass List<PreVisitor>" successfully called.');
                        },
                        failure: function (response) {
                            $('#result').html(response);
                        }
                    });

                })
3
  • $('.form-control') since you have a class in your inputs Commented Nov 2, 2016 at 13:26
  • what i wanted is create different div with class model and iterate thorugh it and each div have different input type which make my single view model .. and I wanted to pass a list of viewmodel to controller Commented Nov 2, 2016 at 13:29
  • try serialize Commented Nov 2, 2016 at 13:30

1 Answer 1

6

Is this is what you need? Get all the input values and put them in an array. Not sure which type of array you want, just values, or values to names.

$('button').on('click',function(){
     var total = [];
     var mapped = {};
     $('.models').find('input[type="text"]').each(function(index,elem){
        total.push(elem.value);
        mapped[elem.name] = elem.value;
     });
     console.log(total);
     console.log(mapped);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="models">
            <div class="row" >

                <div class="col-md-12" >
                    <label class="control-label" >NIC</label>
                    <input type="text" class="form-control" name="VNIC" id="VNIC" required>
                </div>
            </div>



            <div class="row">
                <div class="col-md-12">
                    <label class="control-label">First Name</label>
                    <input type="text" class="form-control" name="VfirstName" id="VfirstName" required>
                </div>
            </div>
</div>
<button>click me</button>

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

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.