JS Click Trunk Button Event Does Not Work

I am new to backbone js and I am struggling to display a warning when I click the button displayed on the html page. I'm sure I'm doing something stupid, but when I press the button, the event does not seem to fire. I tried using both submit and click in the events section, but I cannot get it to work. I would really appreciate if anyone could help me, thanks!

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Event Test</title>
  <script src="../../external/jquery.js"></script>
  <script src="../../external/underscore.js"></script>
  <script src="../../external/backbone.js"></script>  
</head> 
<body>
<div id="standard-input-form"></div>
<script>

  var MovePalletView = Backbone.View.extend({
    initialize: function() {
  },

  events: {
    'submit' : 'move'
  },

  render: function(event) {
    this.$el.append('<button type="button" value="Submit"></button>');
    $("#standard-input-form").html(this.$el.html());
    return this;
  },

  move: function() { 
    alert("You clicked it");
    }
  });

  $(function(){ 
    var movePalletView = new MovePalletView()
    movePalletView.render();      
   })

  </script>    
</body>
</html>
+4
source share
2 answers

The Render function only adds an HTML string that has no related events. Instead, add the HTML element (just remove the part .html()):

events: {
    'click' : 'move'
},

render: function(event) {
    this.$el.append('<button type="button" value="Submit"></button>');
    $("#standard-input-form").html(this.$el);
    return this;
},

, click . - MovePalletView #standard-input-form this.$el:

var MovePalletView = Backbone.View.extend({
    el: '#standard-input-form',
    initialize: function () {},

    events: {
        'submit': 'move'
    },

    render: function (event) {
        this.$el.append('<button type="submit" value="Submit"></button>');
        return this;
    },

    move: function (e) {
        e.preventDefault();
        alert("You clicked it");
    }
});

. , , type="submit", onsubmit. View (el: '#standard-input-form'). onsubmit.

+1

, - , .

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Dairy Tracker</title>
  <script src="../../external/jquery.js"></script>
  <script src="../../external/underscore.js"></script>
  <script src="../../external/backbone.js"></script>  
  <script src="src/MovePallet.js"></script>  
</head> 
<body>
<form id="standard-input-form"></form>
<script>

var MovePalletView = Backbone.View.extend({
    el: '#standard-input-form',
    initialize: function () {},

    events: {
        'submit': 'move'
    },

    render: function (event) {
        this.$el.append('<button type="submit" value="Submit"></button>');
        return this;
    },

    move: function (e) {
      e.preventDefault(); //this line keeps the page from refreshing after   closing the following alert.
      alert("You clicked it");
    }
});

$(function(){ 
  var movePalletView = new MovePalletView()
  movePalletView.render();      
});

</script>

</body>
</html>
+2

Source: https://habr.com/ru/post/1616758/


All Articles