【发布时间】:2020-01-06 06:42:18
【问题描述】:
我一直在尝试从循环表索引中获取值。我想在input 字段中显示它。我可以发送一个字符串,但循环索引不显示在字段中
<div class="form-group">
<label for="PersonName">Person Name</label>
<input type="text" class="form-control" id="PersonName" name="PersonName" placeholder="Name">
</div>
<div class="form-group">
<label for="PersonName">Person Age</label>
<input type="text" class="form-control" id="PersonAge" name="PersonAge" placeholder="Age">
</div>
$(function () {
$.ajax({
type: "get",
url: "/Person/getTable",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
debugger
var i = 1;
var j = 1;
$(data).each(
function () {
$('#mytable').append('<tr><td>' + this.Id + '</td><td>' + this.name + '</td><td>' + this.age + '</td><td>' + this.country + '</td><td>' + this.city + '</td><td> <button class="delbtn" id= "' + this.Id + '"> Delete </button> <button class="editbtn" id= "' + this.Id + '"> Edit </button> </td></tr>')
if (i == data.length)
{
$(".delbtn").click(function () {
var del = $(this).attr('id');
$.ajax({
type: "get",
url: "/Person/delRow",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { del: del },
success: function (data) {
alert(data)
},
failure: function (errMsg) {
alert("failure")
}
})
});
$(".editbtn").click(function () {
var del = $(this).attr('id');
$('#PersonName').val(''); // this is where i want to throw it to the input field
$('#PersonAge').val();
});
}
i = i + 1;
})
}
})
});
【问题讨论】:
-
什么是
data?i没有在任何地方定义。您还给edit和delete按钮提供了相同的id,这样会导致问题。 -
在提出问题时,请设身处地为我们着想。你有足够的信息吗?提供的代码中的所有元素都定义了吗?正如上面的评论所提到的,你在代码和问题中有很多问题。请查看并相应更改
-
我刚刚更新了脚本。抱歉,我没有提供足够的信息
-
我要做的是从表的某些条目的索引中选择编辑按钮,然后将姓名或年龄传递到输入字段
-
我希望你明白我想说的。抱歉,我是新手
标签: jquery html ajax asp.net-mvc