2
votes

I use bootstrap modal dialog. My modal:

<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Hello</h4>
            </div>
            <div class="modal-body">
                Your name:
                <input id="colorData" name="colorData" type="hidden" value=""/>
                <input id="nameData" name="nameData" class="input-lg" type="text" value=""/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button id="postAnswer" type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
    </div>
</div>

I have buttons:

    <div align="center" style="margin-bottom: 20px">
        <button value="all" type="button" class="btn btn-lg btn-default">All</button>
        <button value="green" type="button" class="btn btn-lg btn-success">Green</button>
        <button value="blue" type="button" class="btn btn-lg btn-info">Blue</button>
    </div>

And i have script:

<script type="text/javascript">
    $(document).ready(function () {   
        $(".btn").click(function () {
            var color = $(this).val();
            $(".modal-body #colorData").val(color);
            // $('#colorData').val() - is not empty
            $("#myModal").modal('show');
        });

        $('#postAnswer').click(function (e) {
            e.preventDefault();
            $.ajax({
                // $('#colorData').val() is empty
                // $('#nameData').val() is not empty (I write this on dialog)
                url: "@Url.Action("WriteAnswer", "Home")?name=" + $('#nameData').val() + "&color=" + $('#colorData').val(),
                type: 'POST',
                data: $('#nameData').serialize(),
                success: function (data) {
                    //alert('successfully submitted');
                }
            });

            $("#myModal").modal('hide');
        });

    });
</script>

After call function $(".btn").click element $('#colorData').val() is not empty.But! If I click button on modal then post empty value and value of input colorData is empty. Why? I wanna post data to controller, but value is reset. Sorry for my English.

1
Because your postAnswer button is also class="btn" (so var color = $(this).val(); is null)user3559349
@StephenMuecke, you right! Thanks )))Denis Bubnov
@DenisBubnov, Better to deal with ID attributes and you must always keep ids unique..Rayon
@StephenMuecke, your answer is correct. Please, post your answer and I choose your answer.Denis Bubnov

1 Answers

1
votes

You <button> element with id="postAnswer" also has class="btn", so when you click it, the $(".btn").click(function () { method is also called and var color = $(this).val(); returns null (because that button does not have a value attribute.

To handle this correctly, give an additional class name to the 3 'color' buttons (say)

<button value="all" type="button" class="color btn btn-lg btn-default">All</button>
<button value="green" type="button" class="color btn btn-lg btn-success">Green</button>
<button value="blue" type="button" class="color btn btn-lg btn-info">Blue</button>

and change the first script to

$(".color").click(function () {
    var color = $(this).val();
    $(".modal-body #colorData").val(color);
    $("#myModal").modal('show');
});