0

I have an collection select:

<select id="diagnose_sicherheit" name="diagnose[sicherheit]" class="hidden-field"
data-id="1377670814577-LN7za">
    <option value="V">Verdacht auf</option>
    <option value="Z">Zustand nach</option>
    <option value="A">Ausschluss</option>
    <option value="G">Gesicherte Diagnose</option>
</select>

I want to trigger an function when the user changes the option. I tried this,but it didn't work:

$("input[name='diagnose[sicherheit]']").change(function () {
    $("#sicherheit").val($('input[name="diagnose[sicherheit]"]:checked').val())
    $("#Scomit").click();
});
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
John Smith
  • 6,105
  • 16
  • 58
  • 109

4 Answers4

3

Using $(this).val() in a select you can retrieve the value selected.
I have change your selector jQuery because you can use the id of your select (id must be unique always is more clean the code).
To fire the click event of your element with id Scomit you can use the method .trigger()

try this:

$('#diagnose_sicherheit').change(function(){
    $("#sicherheit").val($(this).val());
    $( "#Scomit" ).trigger('click');
});
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

Use $(this) to get the actual value of the selection

$("input[name='diagnose[sicherheit]']").change(function() {
  $("#sicherheit").val($(this).val());
  $( "#Scomit" ).click();
});
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

Try this

You are using input in Jquery selector that is wrong. It is Select

$(function(){
    $("select[name^='diagnose']").change(function() {
        alert($(this).val());
        $("#sicherheit").val($(this).val())
        $( "#Scomit" ).click();
    });
});

Demo

Amit
  • 15,217
  • 8
  • 46
  • 68
0

Use :

var value = $("#diagnose_sicherheit option:selected" ).val();
var text  = $("#diagnose_sicherheit option:selected" ).text();
alert("Value : "+value+" \nText : "+text);

Fiddle : http://jsfiddle.net/NgNLQ/2/

Cheers

Roy M J
  • 6,926
  • 7
  • 51
  • 78