简体   繁体   English

如何使用jQuery在页面上显示特定的div ID?

[英]How to show particular div id on the page using jquery?

I want to show a particular div with particular id on the page. 我想在页面上显示具有特定ID的特定div。 I am able to show the div with class information but when I try to access it by giving div id then it is not working, this is how am trying to access it from javascript. 我可以显示div的类信息,但是当我尝试通过提供div id来访问它时,则它不起作用,这就是尝试从javascript访问它的方式。

if((document.getElementById("apMain.stageId").value) == "11"){
            doShow();
    }else{
      alert('else');
    }
    function doShow(){
            $(".bill_info").show();
      }

I want to access div with id= stg_Pdel , I have tried using 我想div with id= stg_Pdel访问div with id= stg_Pdel ,我尝试使用

function doShow(){
        $('#stg_Pdel').show();
  }

but am not able to get the div open on the page, what should be the right approach of doing it ? 但无法在页面上打开div,正确的做法是什么?

My div is: 我的div是:

<div class="bill_info">
<h3>Additional required fields</h3>
    <div id="stg_install">
        <div class="row">
            <span class="label">
                <form:label path="billingInfo.otc" cssClass="normalText" cssErrorClass="normalTextRed" >OTC:</form:label>
            </span>
            <span class="formw">
                <form:input path="billingInfo.otc" cssClass="normalField" cssErrorClass="validationError" size="25" disabled='true'/>
            </span>
            <span class="error">Values for all charge fields must be numeric.</span>
        </div>
    </div>

    <div id="stg_Pdel">
        <div class="row">
            <span class="label">
            <form:label path="billingInfo.priceId" cssClass="normalText" cssErrorClass="normalTextRed" >Price Type:</form:label>
            </span>
            <span class="formw">
                <form:select path="billingInfo.priceId" cssErrorClass="validationError" disabled='true'>
                    <form:options items="${priceTypes}" itemValue="key" itemLabel="value" />
                </form:select>
            </span>
        </div>
    </div>

    <div id='stg_closed'>
        <div class="row">
            <span class="label">
                <form:label path="billingInfo.billingClassId" cssClass="normalText" cssErrorClass="normalTextRed" >Billing:</form:label>
            </span>
            <span class="formw">
                <form:select path="billingInfo.billingClassId" cssErrorClass="validationError" disabled='true'>
                    <form:option value="" label="--Select--" />
                    <form:options items="${billingTypes}" itemValue="key" itemLabel="value" />
                </form:select>
            </span>
        </div>
    </div>

    <div style="clear:both"></div><br/>
</div>

You're missing a closing </div> tag right before that one, so it's inside the previous one ("stg_install"). 您在该标记之前缺少</div>标记,因此它位于前一个标记(“ stg_install”)内。 An outer "display: none" will override (sort-of) the "display: block" (or whatever) on the inner <div> . 外部的“ display:none”将覆盖(排序)内部<div>上的“ display:block”(或其他)。

If it's not the <div> nesting issue that was present in the initial post of the question, then perhaps there's a hint of the problem where you mention showing the <div> with class "bill_info". 如果问题的最初帖子中没有出现<div>嵌套问题,那么可能有一个问题提示,您提到使用类“ bill_info”显示<div> If that whole thing is currently not showing, then calling .show() on another <div> (the "stg_Pdel" <div> ) will have no effect. 如果当前未显示全部内容,则在另一个<div> (“ stg_Pdel” <div> )上调用.show() )将无效。 Again, the outer block will hide it no matter what it's "display" style is set to. 同样,无论“显示”样式设置为什么,外部块都将隐藏它。

If the ".bill_info" <div> is hidden, you need to do something like this: 如果“ .bill_info” <div>隐藏,则需要执行以下操作:

function doShow(stage) {
  $('.bill_info').show()
    .children('div').hide().end()
    .find('#' + stage).show();
}

You'd pass "doShow" either "stg_Pdel", "stg_install", or "stg_closed", and it would make sure one of those inner <div> elements is showing. 您将通过“ doShow”传递“ stg_Pdel”,“ stg_install”或“ stg_closed”,这将确保显示这些内部<div>元素之一。 edit for more detail — The idea is to do the following things: 编辑以获取更多详细信息 —目的是执行以下操作:

  1. Make sure that the over all "bill_info" block is visible; 确保所有“ bill_info”块均可见。
  2. Find the immediate child <div> elements and hide them all; 查找直接子元素<div>并将其全部隐藏;
  3. Find the target "stg" <div> and make it visible 找到目标“ stg” <div>并使其可见

Note that using .find() is not really necessary because you're searching by "id" value, but it should work. 请注意, .find()并不需要使用.find()因为您正在按“ id”值进行搜索,但它应该可以工作。

If you need the function to work on more than one stage, you could do it like this: 如果您需要该功能在多个阶段中工作,可以这样做:

function doShow() {
  $('.bill_info').show()
    .children('div').hide();

  for (var ac = 0; ac < arguments.length; ++ac) {
    var stg = arguments[ac];
    $('#' + stg).show();
  }
}

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

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