gpt4 book ai didi

javascript - 从模态中获取输入值

转载 作者:行者123 更新时间:2023-12-03 12:28:52 25 4
gpt4 key购买 nike

我对 modal 和 JQuery 都很陌生,并且在获取两个输入值时遇到了困难。

View 中的模态

<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 的所有搜索功能(前、下、最近、查找等...)的工作方式

如果有人可以帮助我解决这个问题,并可能向我指出有助于阐明所有这些搜索功能的好文章,我将永远感激不已! :)

最佳答案

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

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

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

关于javascript - 从模态中获取输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com