0

I have a tag with some bootstrap properties and I want to select an spcific option when I press Edit link, but at now It doesn't work, other fields works but selects always get default option.

Select

<div class="form-group">
    <select id="categoryField" form="formPerson" class="form-control select2me" placeholder="Categor&iacute;a" th:field="*{category}">                              
        <option value="-1">Categor&iacute;a</option>
        <option th:each="cat : ${categorias}" th:value="${cat.id}" th:text="${cat.name}"></option>                                                              
    </select>
</div>

Script

function editJury(year, category, juryType, accepted, acceptDate, notes, index){
    $('#categoryField option[value="'+category+'"]');
    $('#yearField').val(year);
    $('#juryTypeField').val(juryType);
    $('#dateField').val(acceptDate);
    if(accepted==1){
        $('#radio6').val(accepted);
    }
    if(accepted==0){
        $('#radio7').val(accepted);
    }
    if(accepted==null){
        $('#radio8').val(accepted);
    }
    $('#notesField').val(notes);
    $('#indexField').val(index);

}

I tried this but doesn't work for me, I think it's because when I inspect the final html bootstrap transform the select tag in a div...

EDIT: I change completly how to edit this, and now I'm thinking will be better not use so much JQuery, so now I have 2 problems, first, I can get the selected value correctly (always stay in default -1) and the modal dialog not opens when page loads.

At now, when you click this

<a class="edit" href="#modal-jury-form" th:href="@{/person/edit_jury/__${status.index}__}" data-toggle="modal">Editar </a>

Launch the controller method:

@RequestMapping(value = "person/edit_jury/{index}", method = RequestMethod.GET)
public String editJury(@PathVariable String index, @ModelAttribute("person") PersonForm personForm,
            RedirectAttributes ra, Model model) {

    Jury jury = personForm.getJuries().get(Integer.parseInt(index));

    personForm.setJuryAux(new JuryForm(jury));

    model.addAttribute("person", personForm);

    return "person/new";
}

Then return to page but modal doesn't load automatically... but if you click in New button, modal loads with the data that I passed except for the tags whose load default option.

Community
  • 1
  • 1
Kratul
  • 158
  • 2
  • 13
  • the line `$('#categoryField option[value="'+SEL1+'"]');` will do nothing at all - what are you expecting it to do? – Jamiec Sep 02 '15 at 13:47
  • Ir was a mistake, sry – Kratul Sep 02 '15 at 14:49
  • it wasnt the `SEL1` I was referring to but that whole line - it still does nothing after your edit - once again, what was that line supposed to be doing? – Jamiec Sep 02 '15 at 15:03
  • If you enter in the link that I said in the question, you can see the correct answer of that question and one of the solutions are this – Kratul Sep 02 '15 at 20:25
  • Did *YOU* actually read the answer you linked - they are giving examples of selectors to use, a selector by itself doesnt do anything (except select an element) - you need to call a method somewhere! Ive tried repeatedly to help you by asking for clarification, you dont seem interested in helping yourself, so Im not bothering anymore. – Jamiec Sep 03 '15 at 07:52
  • When I press the link it calls editJury function whose should preselect options sent by me, I don't understand what isn't clear... – Kratul Sep 03 '15 at 07:59

2 Answers2

1

You can just set the value of the select list based on the value of the option you want selected.

$(function() {
  $("#colours").val("blue");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <select id="colours">                        
        <option value="black">Black</option>
        <option value="white">White</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
    </select>
</div>
  • I tried it, If you read my JQuery code, you can see a `#juryTypeField` whose is a select like `#categoryField` – Kratul Sep 02 '15 at 13:43
  • @Kratul #juryTypeField is not in your sample code, can you update the html sample? –  Sep 02 '15 at 13:47
  • It's the same code and same problem, I think it's innecesary – Kratul Sep 02 '15 at 13:59
  • Are you sure your js framework your using is not interfering by doing some data binding? –  Sep 03 '15 at 14:29
0

Final solution was this:

$("#idComboField").select2('data',  id : '-1',  text : 'Cargo');
Kratul
  • 158
  • 2
  • 13