【问题标题】:jquery script is not working on clicking on buttonjquery脚本在单击按钮时不起作用
【发布时间】:2016-09-09 13:14:28
【问题描述】:

我正在尝试从表中获取选定的数据,我正在使用引导程序和 jquery。选择复选框并单击添加到购物车按钮后,我必须从表中获取选定的数据。下面是我的代码 sn-p:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    	<title>Test</title>
    	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.js"></script>
    	<script type="text/javascript">
    	var checkedRows = [];
    	$('#eventsTable').on('.bs-checkbox', function (e, row) {
    		checkedRows.push({id: row.id, name: row.name, forks: row.forks});
    	  console.log(checkedRows);
    	});
    
    	$('#eventsTable').on('uncheck.bs.table', function (e, row) {
    	  $.each(checkedRows, function(index, value) {
    	    if (value.id === row.id) {
    	      checkedRows.splice(index,1);
    	    }
    	  });
    	  console.log(checkedRows);
    	});
    
    	$("#add_cart").click(function() {
    	  $("#output").empty();
    	  console.log(checkedRows);
    	  $.each(checkedRows, function(index, value) {
    	    $('#output').append($('<li></li>').text(value.id + " | " + value.name + " | " + value.forks));
    	  });
    	});
    	</script>
    </head>
    
    <body>
    	<div style="position: absolute;bottom: 42px; padding-left:10px;  "> 
    	 <table id="eventsTable"
           data-toggle="table"
           data-height="300"
           data-url="https://api.github.com/users/wenzhixin/repos?type=owner&sort=full_name&direction=asc&per_page=100&page=1"
           data-pagination="true"
           data-search="true"
           data-show-refresh="true"
           data-show-toggle="true"
           data-show-columns="true"
           data-toolbar="#toolbar">
        <thead>
        <tr>
            <th data-field="state" data-checkbox="true"></th>
            <th data-field="name">Name</th>
            <th data-field="stargazers_count">Stars</th>
            <th data-field="forks_count">Forks</th>
            <th data-field="description">Description</th>
        </tr>
        </thead>
    </table>
    
    <button id="add_cart">Add to card</button>
    <ul id="output"></ul>
    </div>
    </body>
    </html>

当我点击我的按钮时,我的 jquery 函数都没有调用。 我已经提到: Getting values of selected table rows in bootstrap using jquery

请帮我解决这个问题。提前致谢。

【问题讨论】:

  • 您有任何控制台错误吗?

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

问题是由于您的 js 在 DOM 准备好之前加载。

您可以做以下两件事之一:

  1. 将带有事件处理程序的脚本标签放在其中

&lt;/body&gt;
  1. 在您的代码周围添加一个文档就绪事件

$(document).ready(function(){
   // your code here
});

【讨论】:

    【解决方案2】:

    像这样使用#add_cart点击功能

    $(document).ready(function(){
        $("#add_cart").click(function() {
            $("#output").empty();
            $("tbody tr").each(function() {
                if($(this).hasClass("selected")){
                    $('#output').append($('<li>'+$(this).find("td:eq(1)").text()+' | '+$(this).find("td:eq(2)").text()+'</li>'));
                }
            });
        });
    });
    

    【讨论】:

    • 嗨,玛尼,如果我检查表格的最后一行,在输出中获得完整的 dom。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多