【发布时间】:2014-04-03 07:16:20
【问题描述】:
当用户将下拉列表从默认值男性更改为女性时,我试图打开一个弹出窗口(一些对话框)。
我使用了上一篇文章中的 JS 代码,但没有任何反应,在我收到的检查元素告诉我没有对话框,知道如何使它工作吗?
我也尝试过发出警报,但是当我更改下拉列表中的选择时也没有任何反应......
我对 JS 和 Jquery 很陌生...
public class Ad
{
public int Name { get; set; }
public string Email { get; set; }
public IEnumerable<SelectListItem> Gender
{
get
{
return new[]
{
new SelectListItem {Value = "M", Text = "Male"},
new SelectListItem {Value = "F", Text = "Female"}
};
}
}
Index.cshtml 代码。
@model IEnumerable<Ad.Models.Ad>
<script src='https://code.jquery.com/jquery-1.11.0.min.js'></script>
<script src='https://code.jquery.com/ui/1.9.2/jquery-ui.min.js'></script>
<script type="text/javascript">
$(document).ready(function () {
$('#M').change(function () {
if ($(this).val() === "F") {
alert("I am an alert box!");
dialog.dialog('open');
}
});
});
</script>
<h3>My APP</h3>
p>
@using (Html.BeginForm())
{
}
@*<br style="margin-bottom:240px;" />*@
@Html.ActionLink("Create", "Create",
null, htmlAttributes: new { @class = "mybtn" })
<p>
</p>
<style type="text/css">
a.mybtn {
background: #fa0088;
}
</style>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DropDownListFor(modelItem => item.Geneder, item.Gender, new { id = "M" })
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
【问题讨论】:
-
你收到警报了吗?
-
没有注意到可能是什么问题......
-
所以我认为第一个问题是
if ($(this).val() === "F") {。注释掉dialog.dialog('open');代码并检查警报。还可以尝试在if语句之前提醒$(this).val()值 -
还有这个
dialog变量是从哪里来的?通常我们必须放置一个带有 id 作为对话框的 div,并且dialog变量等于$('#dialog') -
我已将其更改为以下代码,但仍然没有任何反应,请您检查一下,让我知道这是否正是您的意思
标签: javascript jquery html asp.net razor