简体   繁体   中英

Dynamic append and remove table rows not working

I work in wordpress with WPMVC.

I am adding table rows dynamically in the form to add a record to the table.

Code:

views/admin/edit.php

<h2>Edit Geozone</h2>

<?php echo $this->form->create($model->name); ?>
<?php echo $this->form->input('geozone_name'); ?>
<?php echo $this->form->input('state'); ?>
<?php echo $this->form->input('ordering'); ?>
<?php echo $this->form->end('Update'); ?>

<!DOCTYPE html>
<html>
<head>
<link data-require="bootstrap@3.2.0" data-semver="3.2.0" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" />
<script data-require="bootstrap@3.2.0" data-semver="3.2.0" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js" data-semver="2.1.1" data-require="jquery@2.1.1"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
  <table class="table table-striped" id="geozoneRule">
    <thead>
      <tr>
        <th colspan="3">
           <input type="button" class="btn btn-success" value="Add Rule" onclick="addGeozoneRule();" />
        </th>
      </tr>
      <tr>
        <th>Country</th>
        <th>Zone</th>
        <th>Remove</th>
      </tr>
    </thead>
  </table>
</body>
</html>


<script>
  var count = 0;

  function addGeozoneRule() {

  var html = '';
  html += '<tr>';
  html += '<td><select name="rule[' + count + '][country_id]"><option value="99">India</option><option value="100">Indonesia</option></select></td>';
  html += '<td><select name="rule[' + count + '][zone_id]"><option value="299">Chennai</option><option value="1100">some</option></select></td>';
  html += '<td><span class="btn btn-danger" onclick="removeTr(this)">Remove <i class="icon icon-remove"></i></span></td>';
  html += '</tr>';

  jQuery("#geozoneRule").append(html);
  count++;
}

function removeTr(element) {
  if (count > 1) {
     jQuery(element).closest('tr').remove();
     count--;
  } else {
    alert('Keep one row');
  }
</script>

I tested the code with plnkr.co and it was successful. But here in my application, it is not even entering the script. What could be the flaw in this code?

Solved the issue by removing the html start and end tags for:

'html' 'head' and 'body'

It works fine now. Thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM