简体   繁体   English

如何使用jquery仅显示包含某些数据的div中的标签?

[英]How to show only the labels in a div which has some data in it using jquery?

I am working with Spring Controller and JSP project. 我正在使用Spring Controller和JSP项目。 I have a jsp page in which I have one button which is Process and once I click that button, it shows me two radio button just below it and a Submit button as well. 我有一个jsp页面,其中有一个按钮是Process ,一旦单击该按钮,它下面就会显示两个单选按钮和一个Submit按钮。

And after clicking Submit button, if everything went fine, then I show like this - 然后,点击“提交”按钮后,如果一切正常,那么我将显示以下内容-

Success= true 
Error= none
Data= some_data

But as you can see in my jsfiddle . 但是正如您在我的jsfiddle中所看到的。 When my form gets loaded after I hit the jsp url it comes like this on the browser - 当我点击jsp网址后加载表单时,它在浏览器中如下所示-

Process

Success= 
Error= 
Data=

Meaning, it has Process button at the top and just below Success , Error and Data label with no values in it as we haven't passed any values yet from the controller. 这意味着,它的顶部和“ Success ,“ Error和“ Data标签的正下方都有“ Process按钮,其中没有任何值,因为我们尚未从控制器传递任何值。 But it looks pretty weird for the users. 但是对于用户来说看起来很奇怪。

So what I did is once I click Process button - I don't show Success , Error and Data labels at all. 所以我要做的就是一旦单击“处理”按钮-我根本不显示SuccessErrorData标签。 It should be shown only after the submit button is clicked with its appropriate value in Success , Error and Data depending on what is passed from the controller. 仅在单击提交按钮之后,才显示该消息,并根据从控制器传递的内容在SuccessErrorData使用适当的值。

So this is the jsfiddle which is working for me. 这就是为我工作的jsfiddle Meaning I only see these labels whenever submit button is clicked and it has some data in it 意味着每当单击“提交”按钮并且其中包含一些数据时,我只会看到这些标签

Now what I am trying to do is, if the value of label Data is null or empty after pressing the submit button, then I don't want to show Data label at all but if the Success and Error label has some data, then I would show them. 现在我想做的是,如果按下提交按钮后,标签Data值为空或为空,那么我根本不想显示Data标签,但是如果SuccessError标签中包含一些数据,那么我会给他们看。 How can I do this if this is possible? 如果可能的话我该怎么办?

I believe that .hide() in jquery do not support the hide for <label> . 我相信jquery中的.hide()不支持<label>的hide。 So I would suggest to use <span> tag for your label instead the <label> tag 因此,我建议为标签使用<span>标签,而不是<label>标签

<span id="status">Success= ${SUCCESS}</span>

Then in your javascript , use .hide() and .show() , something similar as below: 然后在你javascript ,使用.hide().show()如下类似的东西:

$(document).ready(function() {

 //hide label
$('#status').hide();
$('#error').hide();
$('#data').hide();

$('.btn-primary').click(function () {
    $('.btn-primary').removeClass('currentButton')
    $(this).addClass('currentButton')
    $('form').hide()
    $("#form_" + $(this).attr('id')).show();
})

$('#submit').click(function(){

    $('#status').show();
    $('#error').show();
    $('#data').show();

});

})

an updated close enough demo 更新的足够接近的演示

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

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