具体的jQuery片段不起作用

时间:2014-06-22 12:14:55

标签: c# jquery asp.net-mvc asp.net-mvc-4

脚本显示正确加载的Test11。 不幸的是,Test22已不再显示。有谁知道这可能导致什么?

查看Details.cshtml,

@model xzy.Models.Album

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

@using (Html.BeginForm("Details", "Store", FormMethod.Post,
    new
    {
        id = "someID",
        data_oneAction = @Url.Action("one")
    }))
{
    <fieldset

        <label for="Formats">Select format</label>

        @Html.DropDownList("Formats", ViewBag.Formats as SelectList,
  new { id = "FormatsID", @class = "xxx" })


    <input type="submit" value="Submit" id="SubmitID" } />

    </fieldset>
}

@Scripts.Render("~/bundles/jquery")
<script src="@Url.Content("~/Scripts/first_file.js")"></script>

脚本first_file.js,

$(function () {

    $('#FormatsID').change(function () {
        alert('Test11');
        var URL = $('#someID').data('oneAction');
        $.getJSON(URL + '/' + $('#FormatsID').val(), function (data) {
            alert('Test22');
        });
    });
});

浏览器控制台,

console

2 个答案:

答案 0 :(得分:2)

这段代码:

var URL = $('#someID').data('oneAction');

返回undefined,因为它应该是:

var URL = $('#someID').attr('data-oneAction');

答案 1 :(得分:1)

试试这个

new
{
    id = "someID",
    data_oneAction = '@Url.Action("one")'
}))

var URL = $('#someID').prop('data_oneAction');