【问题标题】:Jquery click to open modal returns " undefined is not a function"jQuery单击打开模式返回“未定义不是函数”
【发布时间】:2014-04-15 07:03:30
【问题描述】:

我正在尝试通过单击鼠标打开一个模式,但我似乎根本无法让它工作。这是我的代码

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript">
    $("#Attack1Id").click(function () {
        var location = $.find("#dialog");
        $(location).dialog({
            title: "Dialog box",
            height: 300,
            modal: true,
            open: function (event, ui) {
                $(this).load("@Url.Action("FileTables", "Card")");
            },
            on: ('click', '.table a', function () {
                alert($(this).closest('tr').find('td:first').text())
            })
        })
    });
</script> 

看起来“对话框”是未定义的函数,但我还想如何创建一个 jquery 对话框?

更新

为了清楚起见,这是我的整个 html 页面

@model CardSite.Models.Card

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js"></script>

@{
    ViewBag.Title = "Card";
}

<h2>New card type</h2>

@using (Html.BeginForm("CreateCard", "Card", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        @Html.LabelFor(m => m.Name)
        <div class="text-box-for">
            @Html.TextBoxFor(m => m.Name)
        </div>
        @Html.LabelFor(m => m.Attack1Id)
        <div class="text-box-for">
            @Html.TextBoxFor(m => m.Attack1Id)
        </div>

        <div id="dialog"></div>

    </div>

    <input type="submit" value="Create" class="btn btn-default" />
}



<script type="text/javascript">

    $(document).ready(function () {
        $("#Attack1Id").click(function () {
            var location = $.find("#dialog");
            $(location).dialog({
                title: "Dialog box",
                height: 300,
                modal: true,
                open: function (event, ui) {
                },
                on: ('click', '.table a', function () {
                    alert($(this).closest('tr').find('td:first').text())
                })
            })
        });
    });
</script>

--- 更新---

我刚刚更新了我的代码,看起来像这样

<script type="text/javascript">

    $(document).ready(function () {
        $("#Attack1Id").click(function () { 
            $("#dialog").dialog({
                title: "Dialog box",
                height: 300,
                modal: true,
                open: function (event, ui) {
                    $(this).load("@Url.Action("FileTables", "Card")");
                },
                on: ('click', '.table a', function () {
                    alert($(this).closest('tr').find('td:first').text())
                })
            });
        });
    });

    $(document).ready(
            $("#dialog").dialog({
                title: "Dialog box",
                height: 300,
                modal: true,
                open: function(event, ui) {
                    $(this).load("@Url.Action("FileTables", "Card")");
                },
                on: ('click', '.table a', function(){
                    alert($(this).closest('tr').find('td:first').text())
                })
            })
        );
</script>

并且模态有效,它出现在页面的开头,我可以关闭然后单击输入框,它会再次出现。但是,如果我关闭模态,重新打开模态然后单击视图,它会发出两个警报,就像它创建了多个警报一样......关于如何解决这个问题的任何想法?

【问题讨论】:

  • 您的图书馆附在哪里?
  • 你是否包含了 jQuery UI?
  • 在html文件的顶部
  • 这里工作正常:jsfiddle.net/IrvinDominin/Mc8CP,网络控制台有什么错误吗?
  • 完全没有。我想知道它有什么问题? :(

标签: javascript jquery


【解决方案1】:

$.find 和 $('selector') 的使用似乎有些出入。

此代码返回 JavaScript Array 对象,因此 jQuery 的任何函数都不能应用于结果集。即使结果集似乎相同。

See this post

您是否尝试过使用$("#dialog") 来选择对话框?

【讨论】:

    【解决方案2】:
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery-ui-1.10.4.custom.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $("#Attack1Id").click(function () { 
            $("#dialog").dialog({
                title: "Dialog box",
                height: 300,
                modal: true,
                open: function (event, ui) {
                    $(this).load("@Url.Action("FileTables", "Card")");
                },
                on: ('click', '.table a', function () {
                    alert($(this).closest('tr').find('td:first').text())
                })
            });
        });
    });
    </script>
    

    更新:

    我觉得您的页面中没有调用您的脚本参考。尝试用下面的代码替换脚本引用。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    

    【讨论】:

    • 仍然得到未定义的函数
    • 即使在顶部有这些行,它仍然声明未定义函数:(
    猜你喜欢
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多