简体   繁体   English

为什么我的div没有出现jquery show

[英]Why is my div not showing up with jquery show

I have code in html like this: 我有像这样的html代码:

<div id="answerTypeSection" style="visibility: hidden">
  <div class="row">
      <div class="col-md-2">adfadfafasdfasdfas</div>
 </div>
 <label class="control-label">adfa</label>
  adfaf
</div>

<select id="answerType" class="form-control">
                            <option></option>
                            <option>Text</option>
                            <option>A or B (Boolean)</option>
                            <option>Numeric</option>
                            <option>Photo</option>
                            <option>Files</option>
                            <option>Multi (combo)</option>
                        </select>

Then in javascript 然后在javascript中

$("#answerType").change(function () {
    var answerType = $('#answerType').val();
    console.log('in');
    var showType;

    switch(answerType) {
        case "Numeric":
            showType = "N";
            console.log('numeric');
            $("#answerTypeSection").show();
            break;
        default:
            showType = "what";
            // hide?
            console.log('hide');
            $("#answerTypeSection").hide();
            break;

    }
 // required 
});

console.log does end up showing 'in' and 'numeric' , so it must be something else that i'm not doing right. console.log最终会显示'in'和'numeric',所以它必须是我做得不对的其他东西。 answerTypeSection should show shouldn't it? answerTypeSection应该显示不应该吗?

Try to get the text of selected option element, 尝试获取所选option元素的文本,

$("#answerType").change(function() {
  var answerType = $('#answerType option:selected').text(), showType;
  switch (answerType) {
    case "Numeric":
      showType = "N";
      $("#answerTypeSection").show();
      break;
    default:
      showType = "what";
      $("#answerTypeSection").hide();
      break;
  }
});

DEMO DEMO

Also, .show() and .hide() would not work with visible:hidden . 此外, .show().hide()不会与工作visible:hidden You have to use display:none for making use of it. 您必须使用display:none来使用它。 If you want to use visible property for sure then you have to change the property by using .css() . 如果要确保使用可见属性,则必须使用.css()更改属性。


And the best approach for this kind of situation would be setting the value attribute for the options of your select element, 对于这种情况,最好的方法是为select元素的选项设置value属性,

<select id="answerType" class="form-control">
  <option value="">---</option>
  <option value="Text">Text</option>
  <option value="A or B (Boolean)">A or B (Boolean)</option>
  <option value="Numeric">Numeric</option>
  <option value="Photo">Photo</option>
  <option value="Files">Files</option>
  <option value="Multi (combo)">Multi (combo)</option>
</select>

And for handling the above html you have to rewrite your Js as below, 要处理上面的html,你必须重写你的Js,如下所示,

$("#answerType").change(function() {
  var answerType = $(this).val();
  var showType;
  switch (answerType) {
    case "Numeric":
      showType = "N";
      $("#answerTypeSection").show();
      break;
    default:
      showType = "what";
      $("#answerTypeSection").hide();
      break;
  }
});

DEMO DEMO

visibility CSS应该通过CSS操纵,如下所示

$("#answerTypeSection").css("visibility","visible")

.show()更改显示样式 - 您正在使用visibility:hidden来隐藏它,这是不同的

You are using .show() function which give display property to element and you are using visibility:hidden in div so your function is working fine but you are using wrong function. 您正在使用.show()函数,它为元素提供显示属性,并且您正在使用visibility:hidden在div中,因此您的函数工作正常,但您使用了错误的函数。

Either use .css('visibility', 'visible'); 要么使用.css('visibility', 'visible'); or using style="display:none" in div. 或者在div中使用style="display:none"

HTML HTML

<div id="answerTypeSection" style="display: none">
  <div class="row">
    <div class="col-md-2">adfadfafasdfasdfas</div>
  </div>
  <label class="control-label">adfa</label>
  adfaf
</div>

<select id="answerType" class="form-control">
  <option></option>
  <option>Text</option>
  <option>A or B (Boolean)</option>
  <option>Numeric</option>
  <option>Photo</option>
  <option>Files</option>
  <option>Multi (combo)</option>
</select>

JS JS

$("#answerType").change(function() {
  var answerType = $('#answerType').val();
  console.log('in');
  var showType;

  switch (answerType) {
    case "Numeric":
      showType = "N";
      console.log('numeric');
      $("#answerTypeSection").show();
      break;
    default:
      showType = "what";
      // hide?
      console.log('hide');
      $("#answerTypeSection").hide();
      break;

  }
  // required 
});

Example : https://jsfiddle.net/yped7no0/ 示例: https//jsfiddle.net/yped7no0/

You div does not shows up because of the visible attribute in following code: 由于以下代码中的visible属性,您不显示div:

<div id="answerTypeSection" style="visibility: hidden">

The easiest way to fix this is to change the above code to this: 解决此问题的最简单方法是将上面的代码更改为:

<div id="answerTypeSection" style="display: none">

Here is the JSFiddle demo 这是JSFiddle演示

The reason why your code did not work is because JQuery show() only changes the display attribute and not the visible attribute that you were using. 您的代码无法工作的原因是因为JQuery show()仅更改display属性而不更改您正在使用的visible属性。

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

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