Jquery does not respond to select / change event

Can someone please tell me where the error is, I just don't see it. 'e1 changed' must be registered on the console when the selection changes.

HTML:

<select name="e1" id="e1">
    <option value="1">1</option>
    <option value="2">2</option>
</select>

JS:

$('#e1').change(function() {
    console.log('e1 has changed');
});

jquery definitely works, since I successfully “get” server data for other elements.

+5
source share
5 answers

This code

$('#e1').change(function() {
    console.log('e1 has changed');
});

most likely it is not in your ready-made document handler, and therefore it starts before your dom element is e1ready. You can create a document handler that will work when your dom elements are ready, for example,

$(function() {
    $('#e1').change(function() {
        console.log('e1 has changed');
    });    
});
+10
source

It works for me. Here is the jsFiddle code -

http://jsfiddle.net/CC5P6/

$(document).ready(function() {
  $('#e1').change(function() {
    alert('e1 has changed');
  });
});
+1

, $(document).ready(:

 $(document).ready(function() {
    $('#e1').change(function() {
       console.log('e1 has changed');
    });
 });
+1

:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    $(function() {
        $('#e1').change(function() {
            console.log('e1 has changed');
        });
    });
</script>

</head>
<body>
<select name="e1" id="e1">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
</body>
</html>

document.ready , .

0

, , NoScript .   , .   , " " ( ) :

<select name="subpos" id="subpos">
<option value="examplel">examplel</option>
<option value="sample">sample</option>
<option value="fortest">fortest</option>
</select>


<script>
    $(function() {
        $('#subpos').change(function() {
            console.log('subpos has changed');
        }); 
        $("#subpos").val('sample');//combo box has change but dont appear in console log. why?
   });
</script>

after launch; see console log you cannot see that "subitem has changed" in the console log. but in action "subposition has changed" what happens? it should be written to the console when the selection changes. Now, choosing this option from the drop-down list, you can see that the log of the club changes

0
source

All Articles