具有值的MVC DropDownList onchange事件 [英] MVC DropDownList onchange event with value

查看:78
本文介绍了具有值的MVC DropDownList onchange事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC还是很陌生,并且我试图在更改值时确定DropDownList的值.目前,传递给我的javascript函数的val变量始终为'undefined'.我已经在谷歌上寻找解决方案,到处都有人说要做我现在正在做的事情.我在做什么错了?

I'm pretty new to MVC and i'm trying to determine the value of a DropDownList when the value is changed. At the moment the val variable being passed to my javascript function is always 'undefined'. I've googled for the solution and everywhere says to do exactly what i'm doing right now. What am i doing wrong?

cshtml:

@Html.DropDownListFor(m => m.NearestDealers
     , new SelectListItem[] 
     { new SelectListItem() {
               Text = "No nearest dealers", Value = "0"
          },
          new SelectListItem() {
               Text = "1", Value = "1"
          },
          new SelectListItem() {
               Text = "2", Value = "2"
          },
          new SelectListItem() {
               Text = "3", Value = "3"
          } 
      }, new { onchange = "onNearestDealersChange(this.Value);"})

还有我的javascript:

And my javascript:

function onNearestDealersChange(val) {
    if (val == 0) {
        $("#dealer-pools").addClass("hidden");
    } else {
        $("#dealer-pools").removeClass("hidden");
    }
}

推荐答案

尝试对"this.Value"使用小写的"v"

Try using a lower case 'v' for 'this.Value'

new { onchange = "onNearestDealersChange(this.value);"}

这篇关于具有值的MVC DropDownList onchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆