Javascript 按编辑按钮获取模式中每一行的ID
这是我的表格代码。当按下“编辑”按钮时,我想获得打开模式中每一行的ID。如何将所选行的ID发送到modal?在我下面的例子中,我只得到第一排ID 谢谢大家的关注 情态形式的JSJavascript 按编辑按钮获取模式中每一行的ID,javascript,php,jquery,bootstrap-modal,Javascript,Php,Jquery,Bootstrap Modal,这是我的表格代码。当按下“编辑”按钮时,我想获得打开模式中每一行的ID。如何将所选行的ID发送到modal?在我下面的例子中,我只得到第一排ID 谢谢大家的关注 情态形式的JS $(“.btn[数据目标=”#myModal'])。单击(函数(){ var columnHeadings=$(“thead th”).map(函数(){ 返回$(this.text(); }).get(); columnHeaders.pop(); var columnValues=$(this.parent().s
$(“.btn[数据目标=”#myModal'])。单击(函数(){
var columnHeadings=$(“thead th”).map(函数(){
返回$(this.text();
}).get();
columnHeaders.pop();
var columnValues=$(this.parent().sibbines().map(function()){
返回$(this.text();
}).get();
var modalBody=$('');
var modalForm=$('');
$.each(列标题,函数(i,列标题){
var formGroup=$('');
formGroup.append(“”+columnHeader+“”);
formGroup.append(“”);
modalForm.append(formGroup);
});
modalBody.append(modalForm);
$('.modal body').html(modalBody);
});
$('.modal footer.btn primary')。单击(函数(){
$('form[name=“modalForm”]')。提交();
});
并在单击按钮时调用此函数
而不是用这个
<button class="btn btn-success" data-toggle="modal" data-target="#myModal" contenteditable="false" value=".$id.">
使用
并在单击按钮时调用此函数
而不是用这个
<button class="btn btn-success" data-toggle="modal" data-target="#myModal" contenteditable="false" value=".$id.">
使用
这就是你能做的。我不是在使用您的数据,而是假设您有足够的了解,可以根据需要使用这些数据。 基本上我只是在耍花招
<table>
<?php $sr=1;
while(your condition){?>
<tr>
<td id="td_<?=$sr++;?>">value you want in model</td>
<td><button id="<?=$sr;?>">Edit</button></td>
</tr>
<?php } ?>
</table>
这就是你能做的。我不是在使用您的数据,而是假设您有足够的了解,可以根据需要使用这些数据。 基本上我只是在耍花招
<table>
<?php $sr=1;
while(your condition){?>
<tr>
<td id="td_<?=$sr++;?>">value you want in model</td>
<td><button id="<?=$sr;?>">Edit</button></td>
</tr>
<?php } ?>
</table>
您不应该为每一行的模式创建html,而应该只创建一个模式,当单击每一行时将使用该模式。 然后,使用click方法中的jquery,您可以为modal设置不同的字段,例如id或其他内容,例如:
$("btn").click(function(event) {
id = event.target.id;
$(#myModalIdField).text = id;
});
您不应该为每一行的模式创建html,而应该只创建一个模式,当单击每一行时将使用该模式。 然后,使用click方法中的jquery,您可以为modal设置不同的字段,例如id或其他内容,例如:
$("btn").click(function(event) {
id = event.target.id;
$(#myModalIdField).text = id;
});
使用以下命令:
首先,给出一个表id=“name”
,
使用您自己的表和数据,如果您使用数据库或普通表;
使用按钮模式作为-
<table id="table">
<thead>
<tr>
<th>Your data </th>
</thead>
<tbody>
<tr>
<td>Your Data </td>
<td><button type="button" class="btn btn-primary" onclick='getValue(".$rindex.")' contenteditable="false" value=".$rindex.""> </td>
<div class=" modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header" id="myModalLabel">
<h4 class="modal-title">Enter Remarks:</h4>
<!-- <button type="button" class="close" data-dismiss="modal">×</button>-->
</div>
<!-- Modal body -->
<div class="modal-body">
<form action="" method="post">
<div class="form-group">
<label for="comment">Remarks:</label>
<textarea class="form-control" rows="3" name="comment" id="comment"></textarea>
</div>
<div class="form-group">
<input type="submit" name="msub" id="" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
</div>
</button>
</td>
</tr>
</tbody>
</table>
你的数据
你的数据
使用以下命令:
首先,给出一个表id=“name”
,
使用您自己的表和数据,如果您使用数据库或普通表;
使用按钮模式作为-
<table id="table">
<thead>
<tr>
<th>Your data </th>
</thead>
<tbody>
<tr>
<td>Your Data </td>
<td><button type="button" class="btn btn-primary" onclick='getValue(".$rindex.")' contenteditable="false" value=".$rindex.""> </td>
<div class=" modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header" id="myModalLabel">
<h4 class="modal-title">Enter Remarks:</h4>
<!-- <button type="button" class="close" data-dismiss="modal">×</button>-->
</div>
<!-- Modal body -->
<div class="modal-body">
<form action="" method="post">
<div class="form-group">
<label for="comment">Remarks:</label>
<textarea class="form-control" rows="3" name="comment" id="comment"></textarea>
</div>
<div class="form-group">
<input type="submit" name="msub" id="" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
</div>
</button>
</td>
</tr>
</tbody>
</table>
你的数据
你的数据
我不明白你的问题。你能解释一下你想要的id是什么,或者是数据库中行的id吗?你的JavaScript代码在哪里?“编辑”按钮在哪里?修复你的缩进,我会考虑看。it@prabhjot我添加了picrures以使我的问题更清楚。@Vaidas我正在尝试创建工作js,js是我的弱点…(我没有理解你的问题。你能解释一下你想要的id是什么或来自数据库的行id吗?你的JavaScript代码在哪里?那“编辑”在哪里按钮?修理你的压痕,我会考虑看。it@prabhjot我添加了picrures以使我的问题更清楚。@Vaidas我正在尝试创建工作js,js是我的弱点。。。(谢谢你的回复。你的代码正在将值传递给模态表单。我需要这个id来在查询中写入它,但你代码中的id只能在模态的标题上看到。你说的是哪一个查询?我的错。我忘了回显它。它工作得很好!谢谢!对不起,这不是我所需要的。这是我在模态
中的查询内容我想获取所选行的名称。谢谢您的回复。您的代码正在将值传递给模式表单。我需要此id将其写入查询中,但您代码中的id仅在模式的标题上可见。您在谈论哪个查询?是我的错。我忘了回显它。它工作正常!谢谢!抱歉,这不完全是我需要的。这是我在内容中的modal
中的查询我想获得所选行的名称。谢谢您的回复。使用类的好主意,现在就试试。@OrkhanBagirov它对您有用吗?如果有效,您可以投票并将我的答案标记为正确。谢谢您的帮助!)谢谢您的回复。使用类的好主意,我现在就试试。@OrkhanBagirov它对您有效吗?如果有效,您可以投票并将我的答案标记为正确。谢谢您的帮助!)上面的代码将在您单击任何按钮时显示行号,请使用引导或您自己的css。上面的代码将在您单击任何按钮时显示行号,请使用引导或您自己的css
var table = document.getElementById('table'), rindex;
for (var i = 0; i < table.rows.length; i++) {
table.rows[i].onclick = function getValue(rindex) {
rindex = this.rowIndex;
$('#myModalLabel').text(rindex);
$('#myModal').modal('show');
}
}