【问题标题】:jquery hiding things in formjquery以形式隐藏东西
【发布时间】:2013-01-16 02:42:54
【问题描述】:

我有一个搜索表单,其中有文本框和下拉菜单,但其中一些不起作用。当我点击一个选项然后隐藏它时它会起作用。如果我选择另一个选项,它会显示备份。

这里是例子。

我有一个文本框,如果您选择某些选项,我会隐藏另一个文本框,然后它会显示或隐藏。如果我选择了一些东西然后去我的导航菜单然后回来形成一个应该隐藏的盒子。这是我用来隐藏东西的代码。我不确定为什么它没有设置回默认值并在页面加载时隐藏它们。这是在剃刀视图页面上。

 @*hidefields - Spec Guide*@


//condition fields for the end user request form make sure you change the field ID to yours. 
$(document).ready(function () {
    //check to see if you are on the end users request page
    if (location.pathname === '/Reporting/Summary') {
        //if (document.location.href='/Tickets/New'){
        //toggle the field ,description and title invisible
        $('label for="CustomFieldValue@field.FieldID":contains(* On what page of the Spec Guide:)').toggle();
        $('input#CustomFieldValue3').parent().toggle();

        //monitor the dropdown field
        $('select#CustomFieldValue2').change(function(endUserselect) {
            //grab the value of the dropdown
            var userSelection = $('select#CustomFieldValue2 option:selected').text();

            //do the compare and toggle the field ,description and title visible 
            if (userSelection === 'Spec Guide') {
                $('label for="CustomFieldValue@field.FieldID":contains(* On what page of the Spec Guide:)').toggle();
                $('input#CustomFieldValue3').parent().toggle();
            }
            //hide them again if the user changes his mind
            else {
                $('label for="CustomFieldValue@field.FieldID":contains(* On what page of the Spec Guide:)').hide();
                $('input#CustomFieldValue3').parent().hide();
            }

        });

    }
});
@*hidefields - Spec Guide*@

【问题讨论】:

    标签: jquery asp.net razor


    【解决方案1】:

    这可能是缓存问题。

    有时当您导航到另一个页面并单击后退按钮时,javascript 不会再次执行。相反,您会看到表单的最新版本。

    我认为对此的替代方案(如果您希望它始终有效)可能是用于重置页面的卸载事件。

    当用户离开页面时,unload 事件被发送到窗口元素。这可能意味着许多事情之一。用户可以单击链接离开页面,或者在地址栏中输入新的 URL。前进和后退按钮将触发事件。关闭浏览器窗口将导致事件被触发。即使是页面重新加载也会首先创建一个卸载事件。

    $(window).unload(function () {
        // Here set the default options/hide/show dom elements for your form
    });
    

    试一试,如果有效,请告诉我。

    【讨论】:

    • 我认为你是对的,但我不确定如何在加载时隐藏它。我做了一个函数来隐藏上面的内容,但仍然这样做。我想我写错了代码。
    • 我试过你的代码和这个
    • 添加那个表单的html和css,我看看能不能帮你用reset方法。
    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2012-09-26
    相关资源
    最近更新 更多