duanbipu7601 2014-07-22 17:22
浏览 94
已采纳

如何在Yii2上添加jQuery

I'm trying to use some jQuery on my website. I tried to use the code from This Question

and i can't find my error. I tried to put he code on layot/main.php and on site/index.php and no result. Here is my simple jQuery Code:

 <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_READY);
?>        

Please Help!!

--- EDIT --- 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);
 ?>   
  • 写回答

1条回答 默认 最新

  • doukuipai8544 2014-07-23 04:32
    关注

    There is mistake with quotes in your JS :

    "select[name="dropdown"]"
    

    Change it to

    $this->registerJs(' $(\'select[name="dropdown"]\').change(function(){
    
    if ($(this).val() == "2")
        alert("call the do something function on option 2");
    
    });', View::POS_READY);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题