Вопрос:

Java servlet sending select value

java html servlets web

33 просмотра

1 ответ

33 Репутация автора

I wrote the code below. I want to get element by id_proizvoda and get rest info of that input in a textarea inside of that servlet if selected.

My Code:

  Statement st = conn.createStatement();
                st.executeQuery("SELECT * FROM prodavnica.proizvodi;");
                ResultSet rs = st.getResultSet();

                out.print("<select id = 'izabrani' name = 'izabrani'>");
                while (rs.next()) {
                    out.print("<option value = '");
                    out.print(rs.getString("id_proizvoda"));
                    out.print("'> ");
                    out.print(rs.getString("ime_proizvoda"));
                    out.print("</option>");
Автор: Ognjen Stanojevic Источник Размещён: 08.11.2017 11:33

Ответы (1)


1 плюс

2808 Репутация автора

Решение

If I understood you correctly, what you want is show details of select option in a text area.

To do so, you need JS on your page to modify text area content depending on the choice of select option.

I created a fiddle to give you an idea. Here it is

Here is the sample HTML code:

<select>
  <option value='' data-matr='tralala'>Select an option...</option>
  <option value=1 data-matr='tralala2'>Option 1</option>
  <option value=2 data-matr='tralala3' selected=''>Option 2</option>
  <option value=3 data-matr='tralala4'>Option 3</option>
  <option value=4 data-matr='tralala5'>Option 4</option>
</select>

<textarea id="result"></textarea>

And here is my sample JS code:

$('select').val(3);

$('select').on('change', function(){
var str="";
$( "select option:selected" ).each(function() {
      str = $( this ).attr('data-matr') + " ";
    });

$('#result').val(str);
});

All you need to do is to modify your java code slightly to add custom attributes into your options and add necessary JS code to your page to facilitate on change events.

Please note that I used JQuery.

I hope it helps.

Автор: Alp Размещён: 09.11.2017 01:17
Вопросы из категории :
32x32