简体   繁体   English

当我使用 .show() 命令时,jQuery 不显示特定的隐藏内容

[英]jQuery doesn't display specific hidden content when I use the .show() command

Below is my HTML and jQuery code.下面是我的 HTML 和 jQuery 代码。 jQuery displays the hidden elements, except for the #image4 and #video4 elements, which stay hidden with if I select image or video . jQuery 显示隐藏元素,除了#image4#video4元素,如果我选择imagevideo ,它们会保持隐藏状态。 All the other elements get displayed correctly.所有其他元素都能正确显示。

Any idea why those two won't show?知道为什么这两个不会出现吗?

JS: JS:

 $(document).ready(function(){
        var fields = jQuery('#image_type');
        var select = this.value;
        fields.change(function () {
            if ($(this).val() == 'image') {
              alert($(this).val());
                $('#images_pop_up').show();
                $('#video1').hide();
                $('#video2').hide();
                $('#video3').hide();
                $('#video4').hide();
                $('#image1').show();
                $('#image2').show();
                $('#image3').show();
                $('#image4').show();
                $(' #image5').show();
              }
              else {
                $('#images_pop_up').show();
                $('#image1').hide();
                $('#image2').hide();
                $('#image3').hide();
                $('#image4').hide();
                $('#image5').hide();
                $('#video1').show();
                $('#video2').show();
                $('#video3').show();
                $('#video4').show();
                $('#image5').show();
              }

        });
    });

HTML: HTML:

<div id ="images_pop_up" style="display:none;">
   <span id="image1"  style="display:none;" ><img src="/uploads/commerce/images/large/easton-163135-wheeled-bag.jpg" border="0" alt="easton-163135-wheeled-bag.jpg" title=" easton-163135-wheeled-bag.jpg " class="productimage"><input type="hidden" name="products_previous_image_lrg" value="easton-163135-wheeled-bag.jpg"><br>Nice bag</span>
   <span  id ="video1" style="display:none;"></span></td>
   <td  class="textSmall" valign="top">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
         <tr>
            <span id="image3"  style="display:none;"><input type="hidden" name="products_previous_image_lrg" value="easton-163135-wheeled-bag.jpg"><input type="hidden" name="products_previous_image_lrg_caption" value="Nice bag"></span>
            <span id ="video3"><input type="hidden" name="products_previous_video_embed"><input type="hidden" name="products_previous_video_embed_caption"></span>
            <td class="textSmall" valign="top"><span id="image4" style="display:none;">New Image File: <input type="text" name="products_image_lrg" value="easton-163135-wheeled-bag.jpg"><br>Image Caption: <input type="text" name="products_image_lrg_caption" value="Nice bag"></span>
               <span id ="video4" style="display:none;">Video Embed field: <input type="text" name="products_video_embed"><br>Video Image Caption: <input type="text" name="products_video_embed_caption"></span>
            </td>
            <td><span id="image5" style="display:none;"> <a href='#' onclick='return false;'><img src="includes/languages/english/images/buttons/button_select.gif" border="0" alt="Select" title=" Select " onclick="popupWindowSelectImg('./images_select.php?imgSize=lrg&inpt=products_image_lrg&div=div_img_lrg&width=200&height=30','lrg','products_image_lrg','div_img_lrg');"></a></span></td>
         </tr>
         <tr>
            <td colspan="2">
               <div id='div_img_lrg'></div>
            </td>
         </tr>
      </table>
   </td>
</div>

What sticks out most to me is that you're copy pasting when you could be putting CSS to work for you by giving them all the same class (or the appropriate groups of elements the same class at least) and then hiding and showing them based on that.对我来说最突出的是,当你可以通过给它们所有相同的类(或至少为相同的类的适当元素组)然后隐藏和显示它们来让 CSS 为你工作时,你正在复制粘贴在那。

Your bug is probably caused by a slight typo somewhere in the selectors or the place where you're setting the ids in the markup.您的错误可能是由选择器中某处或您在标记中设置 id 的位置的轻微拼写错误引起的。

try like this像这样尝试

    $(document).ready(function () {
    var fields = jQuery('#image_type');
    var select = this.value;
    $('#image_type').change(function () {
        if ($(this).val() == 'image') {
            alert($(this).val());
            $("[id^=image]").show();
            $("[id^=video]").hide();
        } else {
            $("[id^=video]").show();
            $("[id^=image]").hide();
        }

    });
});

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

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