自动完成和 $.getJSON 问题

发布于 2024-10-10 13:49:16 字数 1828 浏览 2 评论 0原文

我有一个脚本:

<script type="text/javascript">
$(document).ready(function(){
    $("#PrincipleMember_IdNumber").autocomplete({
        close: function(event, ui) {
            var member = {};
            member.IDNumber = $("#PrincipleMember_IdNumber").val();
            $.getJSON("<%= Url.Action("MemberLookup","Member") %>", member, function(data) {
                $("#PrincipleMember_Firstname").val(data.FirstName);
            });
        }
    });
});

表格:

<fieldset class="fieldsetSection">
<legend>Principle Member</legend>
<table>
    <tr>
        <td width="150px" class="editor-label"><%=Html.LabelFor(l=>l.PrincipleMember.IdNumber)%></td>
        <td class="editor-field"><%= Html.AutoCompleteTextBoxFor(i => i.PrincipleMember.IdNumber, "IdNumber", "AutoComplete")%></td>
        <td><%=Html.ValidationMessageFor(v => v.PrincipleMember.IdNumber)%></td>
    </tr>
    <tr>
        <td width="150px" class="editor-label"><%=Html.LabelFor(l=>l.PrincipleMember.Firstname)%></td>
        <td class="editor-field"><%=Html.TextBoxFor(t => t.PrincipleMember.Firstname)%></td>
        <td><%=Html.ValidationMessageFor(v => v.PrincipleMember.Firstname)%></td>
    </tr>
</table>

最后是 json 结果操作:

public JsonResult MemberLookup(Member member)
{
    member = _memberRepository.GetMember(member.IDNumber);
    return this.Json(member);
}

我的 json 结果完美执行,并且得到了结果,但由于某种原因,这部分脚本未执行: $("#PrincipleMember_Firstname").val(data.FirstName);

我尝试用 alert(); 替换它,但这也没有执行。

谁能看到我在这里做错了什么吗?

I have a script:

<script type="text/javascript">
$(document).ready(function(){
    $("#PrincipleMember_IdNumber").autocomplete({
        close: function(event, ui) {
            var member = {};
            member.IDNumber = $("#PrincipleMember_IdNumber").val();
            $.getJSON("<%= Url.Action("MemberLookup","Member") %>", member, function(data) {
                $("#PrincipleMember_Firstname").val(data.FirstName);
            });
        }
    });
});

A form:

<fieldset class="fieldsetSection">
<legend>Principle Member</legend>
<table>
    <tr>
        <td width="150px" class="editor-label"><%=Html.LabelFor(l=>l.PrincipleMember.IdNumber)%></td>
        <td class="editor-field"><%= Html.AutoCompleteTextBoxFor(i => i.PrincipleMember.IdNumber, "IdNumber", "AutoComplete")%></td>
        <td><%=Html.ValidationMessageFor(v => v.PrincipleMember.IdNumber)%></td>
    </tr>
    <tr>
        <td width="150px" class="editor-label"><%=Html.LabelFor(l=>l.PrincipleMember.Firstname)%></td>
        <td class="editor-field"><%=Html.TextBoxFor(t => t.PrincipleMember.Firstname)%></td>
        <td><%=Html.ValidationMessageFor(v => v.PrincipleMember.Firstname)%></td>
    </tr>
</table>

and finally a json result action:

public JsonResult MemberLookup(Member member)
{
    member = _memberRepository.GetMember(member.IDNumber);
    return this.Json(member);
}

my json result is executed perfectly and i get a result, but for some reason this section of the script is not executing: $("#PrincipleMember_Firstname").val(data.FirstName);

i've tried replacing it with an alert();, but that too is not executing.

Can anyone see what i am doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

音盲 2024-10-17 13:49:16

我最好的猜测是(假设您的服务器端方法肯定执行成功,并返回 200 OK )您的数据对象可能没有 FirstName 属性。

如果您使用 Firefox,则获取 FireBug,并检查数据对象的值。如果您使用的是 Chrome - 检查开发者控制台 (Ctrl + Shift + I)

$("#PrincipleMember_Firstname").val(data.FirstName); 替换为 console.log(data ); 并检查 javascript 控制台(firebug 的控制台选项卡,或 Chrome 的开发者控制台 -> 控制台选项卡。您应该能够看到您的对象,以及您可能遇到的任何 js 错误。

如果对象返回正常 -尝试 console.log(data.FirstName);

如果该对象不在控制台中 - 那么您的成功方法没有执行,也许服务器端调用不成功 - 尝试替换它。使用 $.ajax 调用,并使用错误事件来查看问题所在。

此外,如果 console.log 返回未定义 - 您的方法调用结果可能没有返回有效的 json 对象,但我认为看看你的代码,这是极不可能的。

My best guess is (assuming that your server-side method is definitely executing successfully, and returns a 200 OK ) that your data object might not have a FirstName property.

If you use Firefox, then get FireBug, and check the value of the data object. If you're using Chrome - check the developer console (Ctrl + Shift + I)

Replace $("#PrincipleMember_Firstname").val(data.FirstName); with console.log(data); and check the javascript console (firebug's console tab, or Chrome's developer console -> console tab. You should be able to see your object, and any js errors that you might have.

If the object returns fine - try console.log(data.FirstName);

If the object isn't in the console - then your success method isn't executing. Maybe the server-side call isn't successful - try replace it with a $.ajax call, and use the error event to see what's wrong.

Also, if console.log returns undefined - your method call result probably didn't return a valid json object, though I think that'd be highly unlikely, looking at your code.

屌丝范 2024-10-17 13:49:16

我不得不改变我的 json 结果:

return this.Json(member, JsonRequestBehavior.AllowGet);

I had to change my json result:

return this.Json(member, JsonRequestBehavior.AllowGet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文