在包含文件中发布后重新选择复选框

时间:2015-07-07 09:52:37

标签: javascript jquery asp.net

我有页面Search.asp(下面的代码)。和Filtered.asp一样,包括Search.asp。

             <%
                Dim CheckForCheckboxes
                CheckForCheckboxes = Request.form("chkBoxes")
                response.write "CheckForCheckboxes" & CheckForCheckboxes
                %>    
                <div  id="ExSearch" name="ExSearch" >
                    <script>
  // on page load check if this page called from POST and have passed checkboxes to select                  
                    var str = '<%=CheckForCheckboxes%>'; // {"Make[]":["AIXAM","CADILLAC","JEEP"],"selCountry[]":["5","4","8"]}
                    if (!str || str.length === 0) {} else {
                        var Checked = JSON.parse(str); 
// alert works here
        // This one not work
                        $("#ExSearch").find('div.list input[type=radio], input[type=checkbox],div.selector select').each(function () {
// alert do not work here
                            var $el = $(this);
                            var name = $el.attr('name');
                            var value = $el.attr('value');
                            if (Checked[name] && Checked[name].indexOf(value) !== -1 ) {$el.prop('checked', true);}
                        });
                    };

     // from here function which select checkboxes and hold them in hidden input field before submit, on submit pass this object with form               
                    $(function() {

                        $('div.list input[type=checkbox], input[type=radio]').on('change',onValueChange);     
                        $('div.selector select').on('change', onValueChange);

                        function onValueChange() {      
                                var Checked = {};
                                var Selected = {};

                            // Hold all checkboxes
                                $('div.list input[type=radio]:checked, input[type=checkbox]:checked').each(function () {
                                    var $el = $(this);
                                    var name = $el.attr('name');
                                    if (typeof (Checked[name]) === 'undefined') {Checked[name] = [];}
                                    Checked[name].push($el.val());
                                });
                            // Hold all dropdowns
                                $('div.list select').each(function () {
                                    var $el = $(this);
                                    var name = $el.attr('name');
                                    if (!!$el.val()) {Selected[name] = $el.val();}
                                });

                        // Put all together to POST 
                            $.ajax({
                                url: '/Search.asp',
                                type: 'POST',
                                data: $.param(Selected) + "&" + $.param(Checked),
                                dataType: 'text',
                                success: function (data) {
            // Put response data to page and reselect checkboxes, this works good
                                    $("#ExSearch").html(data).find('div.list input[type=radio], input[type=checkbox],div.selector select').each(function () {
                                        var $el = $(this);
                                        var name = $el.attr('name');
                                        var value = $el.attr('value');
                                        if (Checked[name] && Checked[name].indexOf(value) !== -1 ) {$el.prop('checked', true);}
                                        if (Selected[name]) {$el.val(Selected[name]);}
                                    });
    // Hold converted object to string values
                                    $("<input type='hidden' value='' />").attr("id", "chkBoxes").attr("name", "chkBoxes").attr("value", JSON.stringify(Checked)).prependTo("#ajaxform");
                                }
                            });
                        };
                    });

                    </script>   

                    <form name="ajaxform" id="ajaxform" action="Filtered.asp" method="POST">
                    </form>
                    </div>

所以如果页面Search.asp开始我检查对象是否通过表单post方法传递,如果通过我需要选择该对象中的复选框。 所以我创建了对象,然后我用Json.stringify将其转换为字符串然后捕获表单post string并使用JSON.parse转换回对象

所以一切看起来都不错,但复选框没有选择,也没有出现错误。

现在出了什么问题?

1 个答案:

答案 0 :(得分:1)

请注意您的代码首先加载,然后加载所有div,以便$("#ExSearch").find(无法找到任何复选框。

尝试将<script></script>代码放在</form>

之后