Keep selected option dynamically loaded

Asked

Viewed 663 times

1

I need to keep the option chosen in the combobox after the page is submitted, I have a function that does this, but only for Estaticas combobox, for dynamic combobox it does not work. I’m already hours researching and I can’t find anything.

function cursos() {

  $.getJSON('https://localhost/uan/curso/pesquisaPor/', {}).done(function(data) {
    $.each(data, function(id, valor) {

      $("#curso").append('<option value="' + valor.id + '">' + valor.nome + '</option>');
    });
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control-group">
  <label class="control-label">Curso<span class="required">*</span>
  </label>
  <div class="controls">
    <select class="span6 m-wrap" id="curso" name="curso">
      <option value="">Selecciona um curso</option>
    </select>


  </div>
</div>

The courses function returns all the courses that are in the database and fills the select, dynamically, it turns out that whenever I send the form I wanted it to mark up the selected option..

  • 1

    What did you try? Add the code to the question.

  • And the code that generates the data for its function JavaScript? That’s where you need to check which option of select was sent and return.

3 answers

3

I believe that an easy way to solve this is by sending the value of select to the page you need.

Let’s assume you get the select thus:

$_POST["lista"];

You can send this variable to the page you want, via POST or via GET.

The moment you’re generating the <option>, you need to check if there is a $_POST["lista"], if yes, use it to mark the option in his select.

Example:

<?php
$lista = array(
  "um" => 1,
  "dois" => 2,
  "tres" => 3,
  "quatro" => 4
);

// Aqui você verifica se a variável $_POST["lista"]
// e não é nula
$selecionado = "tres";
?>

<select name="opcoes">

<?php
foreach($lista as $chave => $valor) {
    if($selecionado == $chave) {
      echo "<option value=".$chave." selected>".$valor."</option>";
    } else {
      echo "<option value=".$chave.">".$valor."</option>";
    }
}
?>
</select>
  • my case is slightly different from that of a look like this the code Function courses() { $.getJSON('https://localhost/uan/course/searchPor/', { }). done(Function (data) { $.each(date, Function (id, value) { $("#stroke").append('<option value="' + value.id + '">' + value.name + '</option>'); }); }); }

  • @Sam, the more code you add to your question, the better, add this code to your question please and also add whatever you think is necessary.

  • here I have the select that automatically fills <select class="span6 m-wrap" id="course" name="course"> <option value="">Selects a course</option> </select>

  • @Sam, you need to then do this check with JavaScript and not PHP, since it is the JavaScript the person responsible for generating the list. You can send the value of the option selected in your data: $.each(data...

  • can give an example to serve as a basis?

  • @Sam, add to your reply, the code of your JavaScript and also the code that generates the return for your Ajax call, please

  • @Sam, without knowing a little bit about your code, it gets very hard to help.

  • I changed the question you can see the code

Show 3 more comments

1

Add another property to JSON that you return from the server. In addition to id and nome you could join selected, boolean.

Then in Javascript it would be:

$.each(data, function(id, valor) {
      var selected = valor.selected ? 'selected="selected"' : '';
      $("#curso").append('<option ' + selected + ' value="' + valor.id + '">' + valor.nome + '</option>');
});

0

Pass a parameter through the URL. And recover via get and check in Javascript if there is an option from select equal to the value recovered by get.

Browser other questions tagged

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