MVC2 中的 Html.DropdownList 帮助器中的 Javascript 不会触发
谁能告诉我为什么在下面的场景中单击按钮时 JavaScript 不会提交?
控制器如下:
public ActionResult TestJavaScrip()
{
TestEntities db = new Models.TestEntities();
ViewData["SortOptions"] = new SelectList(db.aspnet_Role, "RoleId", "RoleName");
return View();
}
视图包含:
<form>
<%= Html.DropDownList("mySelect",(SelectList)ViewData["SortOptions"],"Please Choose a Role...") %>
<input type="button" onclick="getIndex()" value="Alert index of selected option"/>
</form>
Site.master head 部分中的 Javascript 如下:
<script type="text/javascript">
function getIndex() {
var x = document.getElementById("mySelect");
alert(x.selectedIndex);
}
</script>
渲染如下:
<form>
<select id="mySelect" name="mySelect"><option value="">mySelect</option>
<option value="133d8e56-XXXX-XXXX-XXXX-35ee34e845aa">Administrator</option>
<option value="7c2a6ed5-XXXX-XXXX-XXXX-fecb42bdeebe">Clerk</option>
<option value="54ebe31d-XXXX-XXXX-XXXX-9821df62f5ed">Client</option>
<option value="63e605eb-XXXX-XXXX-XXXX-b007189a41e5">CPA</option>
<option value="fb644c83-XXXX-XXXX-XXXX-5da4b2dc64a0">Executive</option>
<option value="65efa138-XXXX-XXXX-XXXX-098d9195a99d">Master Administrator</option>
<option value="0c863774-XXXX-XXXX-XXXX-6c8d2418ca6b">Part Qualified Accountant</option>
</select>
<input type="button" onclick="getIndex()" value="Alert index of selected option"/>
</form>
为什么它不触发?我希望有人能看到我的明显疏忽。
Can anyone tell me why the javascript won't submit when the button is clicked on the below scenario?
The controller is like so:
public ActionResult TestJavaScrip()
{
TestEntities db = new Models.TestEntities();
ViewData["SortOptions"] = new SelectList(db.aspnet_Role, "RoleId", "RoleName");
return View();
}
The View contains:
<form>
<%= Html.DropDownList("mySelect",(SelectList)ViewData["SortOptions"],"Please Choose a Role...") %>
<input type="button" onclick="getIndex()" value="Alert index of selected option"/>
</form>
Javascript in the Site.master head section is as follows:
<script type="text/javascript">
function getIndex() {
var x = document.getElementById("mySelect");
alert(x.selectedIndex);
}
</script>
Rendering as follows:
<form>
<select id="mySelect" name="mySelect"><option value="">mySelect</option>
<option value="133d8e56-XXXX-XXXX-XXXX-35ee34e845aa">Administrator</option>
<option value="7c2a6ed5-XXXX-XXXX-XXXX-fecb42bdeebe">Clerk</option>
<option value="54ebe31d-XXXX-XXXX-XXXX-9821df62f5ed">Client</option>
<option value="63e605eb-XXXX-XXXX-XXXX-b007189a41e5">CPA</option>
<option value="fb644c83-XXXX-XXXX-XXXX-5da4b2dc64a0">Executive</option>
<option value="65efa138-XXXX-XXXX-XXXX-098d9195a99d">Master Administrator</option>
<option value="0c863774-XXXX-XXXX-XXXX-6c8d2418ca6b">Part Qualified Accountant</option>
</select>
<input type="button" onclick="getIndex()" value="Alert index of selected option"/>
</form>
Why is it not firing? I hope someone can see a glaring oversight on my part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用 MVC 2,我假设您可以访问它附带的 JQuery 库。
这里有一些 JQuery 可以解决您的问题,还有 jsfiddle 这样您就可以看到它的实际效果!
你会发现 jsfiddle 是这类事情的救星,如果你想要一个直接的 javascript 解决方案,你也可以在那里添加一个。
Since your using MVC 2 I assume you have access to the JQuery library that comes with it.
Here is some JQuery that will solve your problem, and a jsfiddle so you can see it in action!
You'll find jsfiddle is a life saver for these sorts of things, if you want a straight javascript solution you can pad one out in there too.