简体   繁体   English

如何通过jQuery隐藏控件组?

[英]How can I invisiblize groups of controls via jQuery?

In my Sharepoint project/Web Part/Web Page, I dynamically create page elements/controls using C# in the *.ascx.cs file. 在我的Sharepoint项目/ Web部件/网页中,我使用* .ascx.cs文件中的C#动态创建页面元素/控件。

In the *.ascx file, I use jQuery for responding to events that happen on the page (selections, changes of checkbox states, etc.). 在* .ascx文件中,我使用jQuery来响应页面上发生的事件(选择,复选框状态的更改等)。

I have a need to conditionally invisiblize groups of controls/elements on the page. 我需要有条件地隐藏页面上的控件/元素组。 Specifically, if the user checks a certain checkbox, I can "remove" whole swaths of the page that, in that scenario, don't apply to her. 具体来说,如果用户选中某个复选框,我可以“删除”该页面的整个行,在该场景中,该行不适用于她。

How can I do this? 我怎样才能做到这一点? I've got this starting point: 我有这个起点:

/* If the select "Yes" (they are seeking payment for themselves, as opposed to someone else), omit (invisibilize) sections 2 and 3 on the form */
$(document).on("change", '[id$=ckbxPaymentForSelf]', function () {
    var ckd = this.checked;
    if (ckd) {
        // what now?
    }
});

I could do it the hair-pulling-out way (which would be very painful for me, because I have almost as much hair as Absalom did), and set each individual element, like so: 我可以做头发拉出的方式(这对我来说非常痛苦,因为我的头发几乎和押沙龙一样多),并设置每个单独的元素,如下:

if (ckd) {
    var $this = $('[id$=txtbxthis]');
    var $that = $('[id$=txtbxthat]');
    var $theother = $('[id$=txtbxtheother]');
    . . . // store a reference to all the other to-be-affected elements in vars
    $this.visible = false; // <= this is pseudoscript; I don't know what the jQuery to invisiblize an element is
    $that.visible = false; // " "
    $theother.visible = false; // " " 
    . . . // invisiblize all the other to-be-affected elements 
}

Surely there's a more elegant/better way! 当然有更优雅/更好的方式!

Is it a matter of assigning all the conditionally invisible elements a particular class, and then invisiblizing every element that is assigned that class, or what? 是将所有条件不可见的元素分配给一个特定的类,然后调用分配给该类的每个元素,或者是什么?

Also, I want the area formerly used by this now-invisible swath to "go away" or "roll up" not sit there with a blank stare, yawning chasm, or Gobi Desert-like featureless expanse. 此外,我希望这个现在看不见的地带以前使用过的区域“走开”或“卷起来”,不要坐在那里,只有一个空白的凝视,打哈欠的峡谷,或戈壁沙漠般的无边无际的广阔。

there are a number of ways to do this. 有很多方法可以做到这一点。 but in your jquery implementation I would decorate the elements with data tags that will tell the code which elements to hide and show. 但是在你的jquery实现中,我会用数据标签来装饰元素,这些数据标签将告诉代码隐藏和显示哪些元素。

<input data-group="1" type="text" />
<input data-group="2" type="text" />

var $group1 = $('*[data-group="1"]');
var $group2 = $('*[data-group="2"]');
if (ckd) {
  $group1.hide(); 
  $group2.show(); 
}
else{
 $group2.hide(); 
 $group1.show(); 
}

You could do the same thing with css classes as well but I prefer using the data attribute 你也可以用css类做同样的事情,但我更喜欢使用data属性

If you can group your controls using classes, you could select the class which needs to be hidden in that particular scenario and just use the hide() function: 如果可以使用类对控件进行分组,则可以选择需要在该特定场景中隐藏的类,并使用hide()函数:

if (ckd) {
    var cls = getClassForCurrentScenario();
    $("." + cls).hide(); //slideUp() would be an animated alternative
}

If the controls can be grouped inside a div , for example, then you'd just need to hide that element: 例如,如果控件可以在div分组,那么您只需要隐藏该元素:

if (ckd) {
    var id = getElementIdForCurrentScenario();
    $("#" + id).hide(); //slideUp() would be an animated alternative
}

It really depends on how you manage to group your controls into "target groups", so that you can efficiently access them later. 这实际上取决于您如何设置将控件分组为“目标组”,以便您以后可以有效地访问它们。

You can hide an element like so: 你可以像这样隐藏一个元素:

$('...').hide();

Or you can slide it up with: 或者你可以用以下方式向上滑动:

$('...').slideUp();

to get a nice sliding up animation. 获得一个很好的滑动动画。

On a side note, you can do this to multiple elements at once, in your case: 另外,在您的情况下,您可以同时对多个元素执行此操作:

$('[id$=txtbxthis], [id$=txtbxthat], [id$=txtbxtheother]').slideUp();

暂无
暂无

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

相关问题 jQuery / Javascript:如何获取动态生成的控件的ClientID - Jquery/Javascript: How can I get ClientID for dynamically generated controls 如何在分页控件中结合使用PHP和jQuery - How can I combine PHP and jQuery in paging controls 我想用很少的控件作为参数在jquery中创建一个自定义函数。 我该如何实现? - I want to create a custom function in jquery with few controls as parameters. How can I achieve this? 如何通过jquery访问以选择动态创建的控件 - How to access via jquery to select controls created dynamically 如何使用jQuery .serialize()获得GET参数的成功和不成功控件? - How can I get successful and not successful controls with jQuery .serialize() for a GET Parameter? jquery:如何通过ajax加载Google Maps API? - jquery: how can i load the Google Maps API via ajax? 如何检查用户是否通过 jQuery selectable 选择了一个项目? - How can I check if a user selected an item via jQuery selectable? 如何在不使用jQuery的情况下以数字输入方式为IE显示微调控件(向上/向下箭头)? - How can I display the spinner controls (up/down arrows) in a number input for IE without using jQuery? 如何使用 javascript/Jquery 在通知托盘中显示我的音乐播放器网站的媒体控件? - how can i show media controls for my music player website in the notification tray using javascript/Jquery? 如何通过jquery访问视图中动态添加的元素 - How can I access dynamically added elements in views via jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM