MVC2 中的 Html.DropdownList 帮助器中的 Javascript 不会触发

发布于 2024-10-04 02:58:58 字数 1685 浏览 2 评论 0原文

谁能告诉我为什么在下面的场景中单击按钮时 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-10-11 02:58:58

由于您使用 MVC 2,我假设您可以访问它附带的 JQuery 库。

这里有一些 JQuery 可以解决您的问题,还有 jsfiddle 这样您就可以看到它的实际效果!

你会发现 jsfiddle 是这类事情的救星,如果你想要一个直接的 javascript 解决方案,你也可以在那里添加一个。

<input type="button" id="btn" value="Alert index of selected option"/>

$(document).ready(function(){
    $('#btn').click(function getIndex() { 
    var x = document.getElementById("mySelect"); 
    alert(x.selectedIndex); 
    });
});

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.

<input type="button" id="btn" value="Alert index of selected option"/>

$(document).ready(function(){
    $('#btn').click(function getIndex() { 
    var x = document.getElementById("mySelect"); 
    alert(x.selectedIndex); 
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文