从模态获取输入值

时间:2014-06-04 14:19:08

标签: javascript jquery twitter-bootstrap modal-dialog

我是modal和JQuery的新手,我很难获得两个输入值。

观看模式

<div class="col-lg-offset-10 col-lg-2">
        <!-- Modal Button-->
        <span class="item-create-button">
            <button class="btn btn-success btn-block" data-toggle="modal" data-target="#createActor">
                <span style="font-size:larger"><span style="margin-right: 5px" class="glyphicon glyphicon-plus"></span>Add New Actor</span>
            </button>
        </span>

        <!-- Modal -->
        <div class="modal fade" id="createActor" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Create</h4>
                    </div>
                    <div class="modal-body">

                        <table class="table" id="createTable">
                            <tr>
                                <th class="col-lg-4">
                                    <h5 style="font-size:x-large"><span style="font-weight:bolder">Name</span></h5>
                                </th>
                                <th class="col-lg-7 col-lg-offset-1">
                                    <input type="text" name="createName" style="text-align: center; height: 35px; font-size: 20px; width: 100%" placeholder="Name" />
                                </th>
                            </tr>
                            <tr>
                                <th class="col-lg-4">
                                    <h5 style="font-size:x-large"><span style="font-weight:bolder">Birthday</span></h5>
                                </th>
                                <th class="col-lg-7 col-lg-offset-1">
                                    <input type="text" name="createBirthday" style="text-align:center; height:35px; font-size:20px; width:100%" placeholder="Birth (mm/dd/yyyy)" />
                                </th>
                            </tr>
                        </table>

                    </div>
                    <div class="modal-footer">
                        <button type="button" onclick="createFunction(this)" class="btn btn-success">Create</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

JQuery“尝试”

function createFunction(element) {
    var name = $(element).prev("div").closest("table").find("#createName").attr('value');
    var name1 = $(element).prev("div").closest("table").find("#createName").val();
    var birth = $(element).prev("table").next("input.col-lg-7").find(":input:first").val();
    var check = $(element).closest("table").attr("class");
    alert(name, name1, birth, check);
}

我已经尝试过找到更多有用的示例,但我只是迷失了所有搜索功能如何适用于JQuery(前,下,最接近,查找等等)

如果有人可以帮我解决这个问题并且可能会指出一篇有助于澄清所有搜索功能的好文章,我将永远感激不尽! :)

1 个答案:

答案 0 :(得分:1)

您可以使用parent()和sibling()关系来获取模型主体,然后选择标准的sizzle

//.parent() is modal-footer
//.parent().prev() is modal-body
//.parent().prev().find("#createName") is the input element
var name = $(element).parent().prev().find("#createName");

我相信你能从这里找到其他人。