I create a text box on the form:

var QueryTextView = Mn.ItemView.extend({ template: $(textQueryTemplate)[0].outerHTML, }); this.querytextView = new QueryTextView(); this.getRegion('selectFieldValue').show(this.querytextView); // отображение текстового поля 

template code:

 <script type="text/javascript"> <input type="text" class="select-field-value" value="Введите значение"/> </script> 

During operation, the words "Enter value" are replaced with the desired word, which I need to receive and process. I'm trying to do it like this:

 var value = this.querytextView.value; 

But this.querytextView no value property. Judging by the log, the template code is stored in the $ el -> 0 -> innerHTML property. How can I pull out the value of this.querytextView ?

  • Thanks for the alarm! She was extremely helpful! - Nicolas Chabanovsky

2 answers 2

Change the View class slightly.

 var QueryTextView; QueryTextView = Mn.ItemView.extend({ template: $(textQueryTemplate)[0].outerHTML, ui: { input: '.select-field-value' }, getValue: function() { return this.ui.input.val(); }, setValue: function(val) { this.ui.input.val(val); return this; } }); 

Now you can easily read / change the value.

 console.log(this.querytextView.getValue()); // -> Введите значение this.querytextView.setValue('bla bla bla'); console.log(this.querytextView.getValue()); // -> bla bla bla 

PS and yes, look here marionettejs

    Textbox value can be obtained using jQuery. In my case it is

     var value = $('.select-field-value').val(); 
    • 2
      Bad practice. For this, the ItemView has a "ui" property through which you can access the DOM elements of a specific ItemView. Further, the problem will get the same if you have several such ItemView (lists, cards, etc.). Do not do this. - Aries Ua
    • I just ran into this just now. Thank! - Mae