Hello, I tried to write a lot of jQuery in my project and didn’t work, I already tried to create a clean and updated project and the simplest jQuery doesn’t work. Here is one example:
<select name="dropdown" size=1>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<?php
use yii\web\View;
$this->registerJs('
$("select[name="dropdown"]").change(function(){
if ($(this).val() == "2")
alert("call the do something function on option 2");
});', View::POS_END);
?>
But the function is in the end of the file like it’s suposed I think
</script><script src="/basic2/web/assets/749ae052/jquery.js"></script>
<script src="/basic2/web/assets/d2986435/yii.js"></script>
<script src="/basic2/web/assets/3b96795f/js/bootstrap.js"></script>
<script type="text/javascript">
$("select[name="dropdown"]").change(function(){
if ($(this).val() == "2")
alert("call the do something function on option 2");
});</script></body>
</html>
Thanks a lot for the reply. I got it work on this way:
<?php
use yii\web\View;
$this->registerJs("
$('select[name="."dropdown"."]').change(function(){
$("."'#teste'".").html('call the do something function on option '+$(this).val());
if ($(this).val() == '2')
alert('call the do something function on option 2');
});", View::POS_READY);
?>