1

假设我有一个包含一些文件字段的表单:

        <form action="" method="post" enctype="multipart/form-data" id="form">
        <h3>Ask </h3>
        <p></p>
            <div>
            <p><label for="id_title">Topic</label>:<p>
            <p><input id="id_title" type="text" name="title" maxlength="300" /><p>
            </div>
            <div>
            <p><label for="id_pic">Picture</label>:<p>
            <p><input type="file" name="pic" id="id_pic" /><p>
            <p><button type="button">Add more pictures</button></p>
            </div>
            <div>
            <p><label for="id_pic_1">Picture 1</label>:<p>
            <p><input type="file" name="pic_1" id="id_pic_1" /><p>
            </div>
            <div>
            <p><label for="id_pic_2">Picture 2</label>:<p>
            <p><input type="file" name="pic_2" id="id_pic_2" /><p>
            </div>
            <div>
            <p><label for="id_pic_3">Picture 3</label>:<p>
            <p><input type="file" name="pic_3" id="id_pic_3" /><p>
            </div>
            <div>
            <p><label for="id_pic_4">Picture 4</label>:<p>
            <p><input type="file" name="pic_4" id="id_pic_4" /><p>
            </div>
            <div>
            <p><label for="id_description">Details</label>:<p>
            <p><textarea id="id_description" rows="10" cols="40" name="description"></textarea><p>
            </div>
            <button type="submit">Submit</button>
        </form>

最初,我想隐藏 pic_1、pic_2、pic_3 和 pic_4,直到用户单击 pic 下的按钮。我认为 javascript 可以做到这一点,但我是 javascript 新手。

谁能给我一些指导?

谢谢。

4

2 回答 2

1

使用 CSS 隐藏图像

.hide-me {
    display: none;
 }

现在在这样的之间包含 jQuery:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

现在在脚本标签之间(最好在页面末尾):

<script>
$(document).on('ready', function () {

    $(.hide-me').show();

});
<script>

这只是告诉 jQuery,一旦页面被加载,你想用 .hide-me 类显示图像

于 2013-03-04T02:39:27.923 回答
1

简短的回答

为您的“添加更多图片”按钮提供一个名为add-more. 然后,将此代码添加到您的页面之前</body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    // Hide the additional photo uploads
    var $additionals = $("#id_pic_1, #id_pic_2, #id_pic_3, #id_pic_4");
    $additionals.hide();
    // Show more photo uploaders when we click 
    $("#add-more").on("click", function() {
        $additionals.show();
    });
});
</script>

长答案

jQuery是一个 JavaScript 库,它使这些事情容易处理。它无处不在而且很有帮助,以至于有些人(包括我自己,在我学习的时候)认为 jQuery就是JavaScript。

在您的页面上包含指向它的链接以获取 jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

在我们做任何其他事情之前,让我们给按钮一个 ID,以便我们以后可以引用它。让我们调用我们的按钮add-more

<button type="button" id="add-more">Add more pictures</button>

现在,让我们在页面中添加一些我们自己的 JavaScript。您可能应该将它放在一个单独的文件中,但是您可以在 jQuery 内容之后<script>将以下内容放在标签中:

$(document).ready(function() {

    // This code is inside of the "document ready" stuff because it
    // will execute AFTER all of the HTML stuff has been loaded.

    // First, let's find all of the additional things we want to hide
    // initially. We COULD do this with CSS but then browsers without
    // JavaScript couldn't upload more photos.
    var $additionals = $("#id_pic_1, #id_pic_2, #id_pic_3, #id_pic_4");

    // Okay, so now we have all of those in a variable called $additionals.
    // Variables don't have to start with a $, but because jQuery uses that,
    // I like to prefix my jQuery-selected elements with a dollar sign.
    // Let's hide them now!
    $additionals.hide();

    // When we click the button with the ID "add-more", show the other stuff.
    $("#add-more").on("click", function() {
        $additionals.show();
    });

});
于 2013-03-04T02:45:17.950 回答