MVC3网站上的jQuery问题,回发后jQuery无法更新hiddenfield(有时) [英] Problems with jQuery on MVC3 website, hiddenfield doesn't get updated by jQuery after postback (sometimes)

查看:135
本文介绍了MVC3网站上的jQuery问题,回发后jQuery无法更新hiddenfield(有时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在MVC3网站上的jQuery脚本有问题.该网页具有一个隐藏域,该隐藏域绑定到MVC模型的属性.当我单击页面的h2元素时,onclick事件将触发一个jQuery函数,该函数将更新隐藏字段.

页面:

Hi,

I have a problem with a jQuery script on my MVC3 Website. The webpage has a hiddenfield that is bound to a property of the MVC model. When I click a h2 element of the page the onclick event will trigger a jQuery function that will update the hidden field.

Page:

@model XxxxxxMVC.Models.ProfielenZoekenModel
@using XxxxxxDLL
@{
    ViewBag.Title = "Zoeken";
}

@using (Html.BeginForm("", "profielen", FormMethod.Post, new { id = "profielen_form" }))
{
    @Html.HiddenFor(m => m.GeselecteerdeOpties)
    
    <!-- ...... some code ...... -->

    <h2  önclick="SwitchSelected('1');">Voorkeur Leeftijd</h2>

    <!-- ...... some code ...... -->

    <button type="submit">TOEPASSEN</button>
}



jQuery脚本:



jQuery Script:

function SwitchSelected(id) {
    var key = "'" + id + "';";
    var ids = jQuery("#GeselecteerdeOpties").val();
    var loc = ids.indexOf(key);

    if (loc >= 0) {
        ids = ids.replace(key, '');
    }
    else {
        ids = ids + key;
    }

    jQuery("#GeselecteerdeOpties").val(ids);

    //alert(jQuery("#GeselecteerdeOpties").val());
}



上面的这段代码即使在回发之后也能很好地工作.但是随后,系统崩溃了,隐藏字段也不再得到更新.

如果再用调试器查看jQuery,我会看到jQuery函数确实得到正确执行,但是最后一个代码"jQuery(#GeselecteerdeOpties").val(ids);根本不会改变hiddenfield.如果再次调试它,我会看到jQuery代码的确看到了已更改的hiddenfield,其中包含了适当的值,尽管该更改无法在页面的HTML中看到.因此,这几乎就像jQuery正在更新内存中的完全不同的页面或类似的东西.


使更糟的是

如果我取消注释最后一个jQuery代码"alert(jQuery(#GeselecteerdeOpties").val());,该问题将不再发生...:S



有任何想法吗?



This code above will work quite well, even after postbacks. But then out no where the system collapses and the hiddenfield doesn''t get updated anymore.

If I then look at the jQuery with a debugger I can see that the jQuery function does get executed properly but the last code "jQuery("#GeselecteerdeOpties").val(ids);" doesn''t change the hiddenfield at all. If I then debug it again I see that the jQuery code does see the changed hiddenfield with proper values in it, although the change can''t be seen in the HTML of the page. So it''s almost like the jQuery is updating a completely different page in memory or someting like that.


To make it even worse;

If I uncomment the last jQuery code "alert(jQuery("#GeselecteerdeOpties").val());", the problem doesn''t occur anymore... :S



Any ideas?

推荐答案

您确定页面中仅定义了一个id为"GeselecteerdeOpties"的html输入吗?似乎您有多个具有相同ID的输入.
尝试通过名称属性更新输入:
Are you sure you have only one html input with id=''GeselecteerdeOpties'' defined in the page? Seems like you have several inputs with same id.
Try updating the input by name attribute:
jQuery("input[name=GeselecteerdeOpties]").val(ids);


这样,您将更新所有GeselecteerdeOpties输入.


Doing so you''ll update all GeselecteerdeOpties inputs.


这篇关于MVC3网站上的jQuery问题,回发后jQuery无法更新hiddenfield(有时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆