使用jQuery.html设置表单和提交按钮

时间:2016-04-08 21:39:38

标签: javascript jquery html twitter-bootstrap

我试图设置用于修改引导表的表单,并且我正在使用JQuery .html()方法。我从Jquery API文档中逐步完成,但无法解释为什么我的保存按钮没有触发。我的错误在哪里?

this is my plunk

JS:

$(function () {

   $("#button").click(function () {
  $('#table').bootstrapTable('refreshOptions', {
                data: data,
                detailView: false,
                filterControl: true,
                columns: [
                    {
                        field: 'name',
                        title: 'Name',
                        sortable: true,
                        editable: true,
                        filterControl: 'input'
                    }, {
                        field: 'stargazers_count',
                        title: 'Structure',
                        sortable: true,
                        editable: true,
                         filterControl: 'input'
                    }, {
                        field: 'forks_count',
                        title: 'Class',
                        sortable: true,
                        editable: true,
                         filterControl: 'input'
                    }, {
                        field: 'description',
                        title: 'Description',
                        sortable: true,
                        editable: true,
                         filterControl: 'input'
                    }
                ]
            });
    });


    $('#table').bootstrapTable({
       detailView: true,
        formatNoMatches: function () {
            return "This table is empty...";
        },

         onClickCell: function(field, value, row, $element) {
                        if (field == 'name') {
                            $element.parent('tr').find('>td>.detail-icon').click();
                             // NOTE: needs index to call to expandRow
                             var $tr = $element.parent('tr');
                             // NOTE: literally first google result: http://stackoverflow.com/questions/469883/how-to-find-the-index-of-a-row-in-a-table-using-jquery
                             var index = $("> tr",  $('#table').find('> tbody')).index($tr);
                             $('#table').bootstrapTable('expandRow',index);
                        }
                    },
      onExpandRow: function(index, row, $detail) {
      // console.log(row)
      $detail.html('<table></table>').find('table').bootstrapTable({
        columns: [{
          field: 'id',
          title: 'id'
        }, {
          field: 'status',
          title: 'stat'
        }, {
          field: 'date',
          title: 'date'
        }],
        data: row.children,
        // Simple contextual, assumes all entries have further nesting
        // Just shows example of how you might differentiate some rows, though also remember row class and similar possible flags
      });
}
});
});

$(function () {
    var $result = $('#form');

   $( "#newTable" ).submit(function( event ) {
                    alert("your changes has been saved");
                });
                $("form").on("submit", function(){
                    alert("form has been submitted.");
                    return false;
                });



    $('#table').on('click-row.bs.table', function (e, row, $element) {

        $('.success').removeClass('success');
        $($element).addClass('success');
        function getSelectedRow() {
            var index = $('#table').find('tr.success').data('index');
            return $('#table').bootstrapTable('getData')[index];
        }
         $result.html(
           '<form action="">' +
            '<table id="newTable" border="0" align="left" style="padding: 10px; margin: 10px; font-size: 14px; color: #0f0f0f">' + '<h3>'+
            '<tr  align="left" style="padding: 10px; margin: 10px;">' + '<td style="font-weight: bold;">Name</td>' + '<td>&nbsp;</td>' + '<td><input type="text" id="frm-name" name="frm-name" value="' + getSelectedRow().name + '"/></td>' + '</tr>' +
            '<tr align="left" style="padding: 10px; margin: 10px;">' + '<td style="font-weight: bold;">Structure</td>'  + '<td>&nbsp;</td>' +  '<td><input type="text" id="frm-structure" name="frm-structure" value="' + getSelectedRow().stargazers_count + '"/></td>'+ '</tr>'+
            '<tr align="left" style="padding: 10px; margin: 10px;"> '+ '<td style="font-weight: bold;">Class</td>' + '<td>&nbsp;</td>' +  '<td><input type="text" id="frm-class" name="frm-class" value="' + getSelectedRow().forks_count + '"/></td>'+ '</tr>'+
            '<tr align="left" style="padding: 10px; margin: 10px;"> '+ '<td style="font-weight: bold;">Description</td>' + '<td>&nbsp;</td>' +  '<td><input type="text" id="frm-description" name="frm-description" value="' + getSelectedRow().description + '"/></td>'+ '</tr>'+
            '</h3>' + '<input style="float: right; margin-top: 20%; margin-right: 15px;" class="btn btn-default" type="button" id="btn-submit-form" value="Save" type="submit"/>'
                        + '</table>' + '</form> '

        )
    });
});

working plunk

2 个答案:

答案 0 :(得分:1)

#newTable 的提交事件是表格,而不是表格。

应该是:

    $(document).on('submit', "#form form", function( event ) {
        alert("Your change has been saved");
    });

答案 1 :(得分:1)

您的保存按钮的当前输入类型是“按钮”而不是“提交”,这就是它未被提交的原因。

在你的table.js的第104行替换你的'现在只需为这个提交事件添加JS / jQuery。