【问题标题】:Open Dialog when drop down was changed更改下拉菜单时打开对话框
【发布时间】: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


【解决方案1】:

通常当没有 id 为“对话框”的 div 时会出现问题。 javascript变量应初始化为dialog = $('#dialog')

<script type="text/javascript"> 
  $(document).ready(function () { 
     $("#M").change(function () { 
          alert($(this).val());  
          alert($(this).val() == "F"); 
     if ($(this).val() == "F") { 
             alert("I am an alert box!");
              //dialog.dialog('open'); //commenting out this line would tell where the problem lies.
       } 
    }); 
  }); 
</script>

更新: 要将其应用于多个选择框,您应该使用类选择器,例如 jQuery 的 .M,而不是 id 选择器 #M。首先我们需要给同一个类M所有的选择框。

@Html.DropDownListFor(modelItem => item.Geneder, item.Gender, new { id = "M", @class = "M" })

现在将$("#M").change(function () { 更改为$(".M").change(function () {

【讨论】:

  • 投票并标记为答案:),关于这个问题的最后一个问题,我有表,为​​什么它只对表的第一个条目起作用?表中第二个条目的下拉菜单不起作用
  • 因为$('#M') 只返回id 为M 的第一个元素。你应该把一些类设置为@Html.DropDownListFor(modelItem =&gt; item.Geneder, item.Gender, new { id = "M", @class = "M" })。现在将$('#M') 更改为$('.M')。现在它应该适用于每个带有 css 类 M 的下拉列表。
【解决方案2】:

$('#M') 替换为$('select')

【讨论】:

  • 无需更改#M 它是下拉列表的 ID。
  • 刚试过jsfiddle.net/4REJn。我认为您的代码在其他地方有问题。我可以知道您使用哪种编码语言吗?
  • 目前我已经创建了 MVC5 应用程序,这段代码在 index.cshtml 视图中
  • 检查您的代码在 viewsource 中的格式是否正确。并在评论对话框代码后检查您是否收到警报。
猜你喜欢
  • 2021-09-11
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
相关资源
最近更新 更多