简体   繁体   English

回发后如何在下拉列表中保留所选值?

[英]How to preserve selected value in drop-down list after postback?

$("#county_id").change(function() {
    $("#vice_county_id").html("<option value=\"\">-- Select One --</option>");
    var co_id = $(this).val();
    if(co_id != 0) {
        $.getJSON('./php/includes/vice_county_web_service.php?co_id=' + co_id,function(data) {   
            $.each(data, function() {
                $("#vice_county_id").append($("<option></option>").val(this['vice_county_id']).html(this['vice_county_name']));
            });
        });
    }
});

Hi Guys, I have the above code for loading options in one drop down based on the selection in another. 嗨,大家好,我有上面的代码,用于根据另一个选项的选择在一个下拉菜单中加载选项。 It works fine but to finish this up I am trying to get it to be sticky. 它工作正常,但要完成此操作,我尝试使其具有粘性。 Ie if the form is posted and county has a value already selected then the vice county pre loads with the relevant options. 即,如果过帐了表格并且县已经选择了值,则副县预加载相关选项。 Basically fire the .change functionality on load. 基本上在加载时触发.change功能。 I have tried the .load and triggerhandler functionality but it does not seem to work. 我已经尝试过.load和triggerhandler功能,但是它似乎不起作用。 Any Ideas?? 有任何想法吗?? Thanks. 谢谢。

As far as I am aware, in order to prepopulate a form, you'll need to use server-side code. 据我所知,为了预填充表单,您将需要使用服务器端代码。 You say that the user posts his code somewhere, but you don't say how the form post is being handled. 您说用户在某个地方发布了他的代码,但是您没有说表单发布是如何处理的。

I use ASP.net where I work, so I would have a server-side property for the value of each drop-down list. 我在工作的地方使用ASP.net,因此每个下拉列表的值都有一个服务器端属性。 Then, when the page renders, I print the value of the property to my JavaScript code. 然后,当页面呈现时,我将属性的值打印到我的JavaScript代码中。 You'll need to make your JavaScript code add the attribute selected='selected' to the option that you want to be selected when the page loads. 您需要使您的JavaScript代码在页面加载时将属性selected='selected'添加到要选择的选项中。

Update: It sounds like you're saying that you select drop-down 1, and then the options for drop-down 2 are automatically populated. 更新:听起来您说的是选择下拉列表1,然后自动填充了下拉列表2的选项。 It sounds like the problem is that drop-down 2 cannot be preserved after the postback because its options are populated after the page loads (via AJAX). 听起来好像是问题在于回发后无法保留下拉列表2,因为它的选项是页面加载后(通过AJAX)填充的。 I believe that the solution to your problem is going to be to set the selected attribute inside code that runs when you successfully get the drop-down options via AJAX. 我相信解决您的问题的方法是在通过AJAX成功获取下拉选项时在运行的代码中设置selected属性。

        $.each(data, function() {
            $("#vice_county_id").append($("<option></option>").val(this['vice_county_id']).html(this['vice_county_name']));
            //Check if the option being added was previously selected.
        });

Use the Cookies to store the value of the drop down change event. 使用Cookies来存储下拉更改事件的值。

and when the form loads use the same cookie to retrieve the value and display the user what ever he selected. 当表单加载时使用相同的cookie来检索值并向用户显示他选择的内容。

But maintaining cookie for this task is not recommeneded. 但是不建议为此任务维护cookie。

You can use the JSTL if its a JSP page. 如果它是JSP页面,则可以使用JSTL。

Cookie creating guidlines http://www.quirksmode.org/js/cookies.html Cookie创建指南http://www.quirksmode.org/js/cookies.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM