Dynamically add Checkbox

Asked

Viewed 401 times

0

I have a project where by clicking a button I intend to pick up the text of 3 TextBox and add them to a CheckBox.

So far so good, the problem arises when I want to add these CheckBox automatic, for the user to add more lines.

I have here an example of what he will introduce in a TextBox:

CheckBoxLinhas.Text = "Quantidade: " + TextBoxQuantidade.Text + " Artigo: " + TextBoxArtigo.Text + " Valor: " + TextBoxValor.Text;

How then can I create the CheckBoxes automatic?

  • as well as automatic?

  • @Rafaelferreira by code, by clicking the button he creates a Chechbox in Webform

  • You just need to add a method to create this checkbox to the event.

1 answer

1


You could use an observable array of Knockout.js to do this.

var ViewModel = function(){
  self = this;
  //inicializa a lista de checkboxs vazia
  self.listaCheckBox = ko.observableArray([]);
  
  
  self.adicionaCheckBox = function(){
   self.listaCheckBox.push("checkbox"); 
   }
   
}

$(document).ready(function () {
        var viewModel = new ViewModel();

        ko.applyBindings(viewModel);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<button type="button" data-bind="click:$root.adicionaCheckBox">Adicionar CheckBox</button>
	<ul   data-bind="foreach: $root.listaCheckBox()">
	  <li>
	  <input type="checkbox"/><p class="form-control-static" data-bind="text: $data"></p>
	  </li>
	</ul>

  • The code performs perfectly here on the site, but I am a fairly new in Asp.net and I did not understand very well where I have to deploy this code.

  • This is a javascript library that you would use in your html. The idea is that you upload the information from your server, treat your information in the view and then by clicking a "save" button for example you would send the information back to the server.

  • Complementing my previous comment, for this you would have to pass your Model to the knockoutJS which can be done in several ways. Here you find a good discussion on this.

  • Right! Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.