Javascript asp.net mvc弹出模式从表行读取
我可以成功地将所选表格行读取到弹出模式,但如果我要将按钮移动到从javascript动态读取表格行,则我的所有字段都将变为空或未读取所选表格 这里是我的Javascript,它与我的按钮更新一起构建我的表行,以调用弹出模式:Javascript asp.net mvc弹出模式从表行读取,javascript,asp.net,asp.net-mvc,bootstrap-modal,Javascript,Asp.net,Asp.net Mvc,Bootstrap Modal,我可以成功地将所选表格行读取到弹出模式,但如果我要将按钮移动到从javascript动态读取表格行,则我的所有字段都将变为空或未读取所选表格 这里是我的Javascript,它与我的按钮更新一起构建我的表行,以调用弹出模式: $.get("/Home/GetItem", function (data) { tempDIM = JSON.parse(data); if (tempDIM[0]["status"] == "SUCCESS") { for (var i
$.get("/Home/GetItem", function (data) {
tempDIM = JSON.parse(data);
if (tempDIM[0]["status"] == "SUCCESS") {
for (var i = 1; i < tempDIM.length - 1; i++) {
$("#TableBody").append("<tr>" +
"<th scope='row'>" + (i + 1) + "</th>" +
"<td id='" + tempDIM[i]["id"] + "'>" + tempDIM[i]["id"] + "</td>" +
"<td>" + tempDIM[i]["name"] + "</td>" +
"<td>" + tempDIM[i]["address"] + "</td>" +
"<td>" + tempDIM[i]["age"] + "</td>" +
"<td>" + tempDIM[i]["status"] + "</td>" +
'<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Update</button></td>' +
"</tr > ");
}
}
});
$.get(“/Home/GetItem”,函数(数据){
tempDIM=JSON.parse(数据);
if(tempDIM[0][“状态”]=“成功”){
对于(变量i=1;i
模态:
<table class="table" style="margin-top:10px">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>age</th>
<th>status</th>
</tr>
</thead>
</table>
<table class="table table-striped" id="tBody">
<tbody id="TableBody"></tbody>
</table>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-target="#exampleModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><b>Update Selected Details</b></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>id:</label>
<input type="text" id="id" disabled />
</div>
<div class="form-group">
<label>name :</label>
<input type="text" id="name" disabled />
</div>
<div class="form-group">
<label>address :</label>
<input type="text" id="address" disabled />
</div>
<div class="form-group">
<label>age:</label>
<input type="text" id="age" disabled />
</div>
<div class="form-group">
<label>status:</label>
<input type="text" id="status" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" id="btnSave" onclick="SaveChanges()" class="btn btn-primary" data-dismiss="modal">Save changes</button>
</div>
</div>
</div>
</div>
身份证件
名称
地址
年龄
地位
更新选定的详细信息
&时代;
身份证件:
姓名:
地址:
年龄:
地位:
接近
保存更改
以及我阅读表格的脚本:
$(function () {
$(".btn").click(function () {
var $row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td"); // Finds all children <td> elements
$("#id").val($row.find('td:eq(0)').text())
$("#name").val($row.find('td:eq(1)').text())
$("#address").val($row.find('td:eq(2)').text())
$("#age").val($row.find('td:eq(3)').text())
$("#status").val($row.find('td:eq(4)').text())
});
});
$(函数(){
$(“.btn”)。单击(函数(){
var$row=$(this).recest(“tr”),//查找最近的行
$tds=$row.find(“td”);//查找所有子元素
$(“#id”).val($row.find('td:eq(0)').text())
$(“#name”).val($row.find('td:eq(1)').text())
$(“#address”).val($row.find('td:eq(2)').text())
$(“#age”).val($row.find('td:eq(3)').text())
$(“#status”).val($row.find('td:eq(4)').text())
});
});
为什么我从弹出模式中获取空值的任何建议或注释。TIA假设您的表包含正确的数据,
$(“.btn”)。单击事件处理程序似乎是错误的,因为您可以在表行之外调用其他按钮(即
)。您应该处理examplemodel
中的show.bs.modal
事件,然后迭代行元素,并将所有值放入按列索引排序的相应
元素中,如下例所示:
$("#exampleModal").on('show.bs.modal', function (e) {
var $button = $(e.relatedTarget);
var $row = $button.closest("tr");
var $tds = $row.find("td");
$.each($tds, function(i, value) {
$("input:eq(" + i + ")").val($(this).text());
});
});
注意:如果您想在模式中提交文本框值,请避免使用禁用的属性来阻止值被发布,请改用只读的,例如
工作示例(简化):
相关问题:
尝试使用$.each()
而不是使用JSON.parse
和for
循环来迭代响应。您在哪一部分获得空值?我在modal中的所有字段都获得空值您的意思是tempDIM[0][“status”]
和其他tempDIM[0][“somekey”]
包含空值而不是所选数据吗?您可以提供GetItem
操作内容吗?my GetItem只是一个查询,返回一个列表并将其转换为jason以便在表行上进行解析。您的示例代码段中不存在
标记,目标表实际存在吗?仍然要找出一些缺失的部分,你没有在这里介绍,但可以使空值。我明白了,非常感谢!