我正在尝试使用bootstrap创建一个可选择的表。有没有办法在单元格内选择元素而不选择表格行?
根据下面的代码片段,有没有办法选择文本框而不选择表格行?
我正在尝试复制jqueryui / selectable
的功能谢谢!
$(function() {
var $table = $('#table');
$table.on('click-row.bs.table', function(e, row, $element) {
alert("Row is selected");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.css" rel="stylesheet"/>
<script src="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.js"></script>
<table id="table"class="table table-hover" data-toggle="table">
<thead>
<tr>
<th>#</th>
<th>Data</th>
<th>User</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>0.52,1.041</td>
<td>Samantha</td>
<td>40%</td>
</tr>
<tr>
<td>2</td>
<td>226,134</td>
<td><input value="Martin"></td>
<td>-20%</td>
</tr>
<tr>
<td>3</td>
<td>0.52/1.561</td>
<td>Damien</td>
<td>26%</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
我认为一个简单的停止预测。尝试:
$("input").click(function(e) {
e.stopImmediatePropagation();
});
$(function() {
var $table = $('#table');
$table.on('click-row.bs.table', function(e, row, $element) {
alert("Row is selected");
});
$("input").click(function(e) {
e.stopImmediatePropagation();
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.css" rel="stylesheet"/>
<script src="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.js"></script>
<table id="table"class="table table-hover" data-toggle="table">
<thead>
<tr>
<th>#</th>
<th>Data</th>
<th>User</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>0.52,1.041</td>
<td>Samantha</td>
<td>40%</td>
</tr>
<tr>
<td>2</td>
<td>226,134</td>
<td><input value="Martin"></td>
<td>-20%</td>
</tr>
<tr>
<td>3</td>
<td>0.52/1.561</td>
<td>Damien</td>
<td>26%</td>
</tr>
</tbody>
</table>
&#13;
答案 1 :(得分:0)
我不确定你为什么要使用外部库这么简单,除非我遗漏了什么,这应该可以解决问题..
$(function() {
var $table = $('#table');
$table.find("tr").click(function(e){
if('INPUT' === e.target.tagName) return;
var sel = 1==$(this).attr('data-selected');
$(this).attr('data-selected', sel?0:1);
$(this).find('td').css('background-color', sel ? "" : 'green');
console.log(getSelections());
});
function getSelections(){
var vals = [];
$table.find("tr[data-selected='1']").each(function(){
var ele = [];
$(this).find('td').each(function(){ ele.push($(this).text()) })
vals.push(ele);
});
return vals;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.css" rel="stylesheet"/>
<script src="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.js"></script>
<table id="table"class="table table-hover" data-toggle="table">
<thead>
<tr>
<th>#</th>
<th>Data</th>
<th>User</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>0.52,1.041</td>
<td>Samantha</td>
<td>40%</td>
</tr>
<tr>
<td>2</td>
<td>226,134</td>
<td><input value="Martin"></td>
<td>-20%</td>
</tr>
<tr>
<td>3</td>
<td>0.52/1.561</td>
<td>Damien</td>
<td>26%</td>
</tr>
</tbody>
</table>
答案 2 :(得分:0)
与上面的答案相同的注释“我曾经摔过一只熊”,你可以得到所选字段的id(考虑到你确实有一个id)。我添加了一个用于演示目的
$(function() {
var $table = $('#table');
$table.on('click', function(e) {
console.log(e.target.id);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.css" rel="stylesheet"/>
<script src="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.js"></script>
<table id="table"class="table table-hover" data-toggle="table">
<thead>
<tr>
<th>#</th>
<th>Data</th>
<th>User</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>0.52,1.041</td>
<td id="1">Samantha</td>
<td>40%</td>
</tr>
<tr>
<td>2</td>
<td>226,134</td>
<td id="2"><input type="text" id="userInput" value="Martin"></td>
<td>-20%</td>
</tr>
<tr>
<td>3</td>
<td>0.52/1.561</td>
<td id="3">Damien</td>
<td>26%</td>
</tr>
</tbody>
</table>