Backbone.js Change Text based on selected dropdown

試著忘記壹切 提交于 2019-12-12 03:26:14

问题


I am trying to get some text to change when the user selects a different option in a dropdown box. I am using Backbone.js and am trying to figure out how to get the text belonging to the track when the user chooses an option.

As a follow-up, if I didn't set the value of the option to track.text, but rather track.title, how would I be able to get it from the .js file? I had originally set track.title and track.text in a JSON file. I am trying to learn how to get information from the JSON files. Thanks!!

window.LibraryLessonView = LessonView.extend({
events: {
    "change .sel " : "changeText"
},

changeText: function() {
//not sure what I should write here...
}


});

This is in my HTML file:

    <script type="text/template" id="lesson-template">
         <span class="lesson-title"><%= title %></span>
         <select class="sel">

//get the tracks from the JSON file and put them all in the dropdown
            <% _.each(tracks, function(track) { %>
              <option value = <%= track.title %> ><%= track.title %></option>


          <% }); %>        
          </select>

<p> Blah </P> //I want to change the text here

        </script>

回答1:


[EDITED] that should work for you:

changeText: function(e) {
    alert(e.target.value); 
}

e.target refers to DOM element that triggers change event here. e.target.value returns value of currently selected item in select box



来源:https://stackoverflow.com/questions/19203037/backbone-js-change-text-based-on-selected-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!