【问题标题】:jquery function fired when dropdownlist selectedvalue下拉列表选择值时触发 jquery 函数
【发布时间】:2012-11-06 04:32:30
【问题描述】:

我有一个 jQuery animate 函数,如果我的下拉列表的选定值是 X,我想调用它

       <script>
    $(function () {
        var state = true;
        ($("#button").click)(function () {
            if (state) {
                $("#effect").animate({
                    backgroundColor: "#aa0000",
                    color: "#fff",
                    width: 500
                }, 1000);
                  } else {
                $("#effect").animate({
                    backgroundColor: "#fff",
                    color: "#000",
                    width:500

                }, 1000);
            }
            state = !state;
        });
    });

【问题讨论】:

  • 这里有很多格式问题,而且这看起来像 .click 事件而不是 .change() 一个下拉列表,就像你说的那样。
  • 我同意事件应该写在下拉列表而不是按钮上
  • 这里有一个例子可以帮助你
    stackoverflow.com/questions/4910795/…

标签: jquery asp.net function drop-down-menu selectedvalue


【解决方案1】:

你可以这样尝试

jQuery(document).ready(function(){
    var state = true;
  $("#otherCatches").change(function() {
      if (state) {
            $("#effect").animate({
                backgroundColor: "#aa0000",
                color: "#fff",
                width: 500
            }, 1000);
              } else {
            $("#effect").animate({
                backgroundColor: "#fff",
                color: "#000",
                width:500

            }, 1000);
        }
        state = !state;

   });
});

【讨论】:

    【解决方案2】:

    DropDown id -> ddlDropDown

          $(document).ready(function(){
              var state = true;
            $("#ddlDropDown").change(function() {
                 if ($("#ddlDropDown").val()=="X") { //Checking if value is X
                    $("#effect").animate({
                         backgroundColor: "#aa0000",
                         color: "#fff",
                         width: 500
                      }, 1000);
                  } else {
                   $("#effect").animate({
                   backgroundColor: "#fff",
                   color: "#000",
                   width:500
    
            }, 1000);
          }
          state = !state;
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      相关资源
      最近更新 更多