簡體   English   中英

單擊按鈕時如何顯示div標簽?

[英]how to show a div tag when i click on a button?

我的程序是上傳文件,當我單擊“ 上傳”時 ,它將調用servlet。 同一上傳頁面包含一些其他字段,當我僅單擊“上傳”按鈕時,這些字段應顯示。 如何調用servlet以及顯示其余內容。 實際上使用servlet,我想在同一頁面上顯示文件內容。

這是我的代碼。

<form class="form-horizontal" name="regist" action="Registration"
            onsubmit="return validateFile((this))" enctype="multipart/form-data"
            method="post">

            <div class="control-group" class="span12">
                <label class="control-label" for="file">Please upload a
                    file:</label>
                <div class="controls">
                    <input type="file" name="file"/>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <button type="submit" class="btn btn-primary">Upload</button>
                </div>
            </div>
        </form>
    </div>

<div id="showFrames" style="display:none;">

<!--
  hidden contents are here -->

</div>

而我的劇本是

function validateFile(form) {
        var fileName = form.file.value;
        if (!fileName) {
            alert("No File Selected!!!");
            return false;
        }
        else
        {
            $("#showFrames").show();
        }
    }

但這不起作用。 誰能幫我? 謝謝

使用jQuery,我會這樣做:

$('.form-horizontal').sumbit(function(e) {
    if($(this).find('input[type="file"]').val().length) { //check by length
        $("#showFrames").show();
    } else {
        alert('No file Selected');
        e.preventDefault();
    }
});

如果您需要多個表單,最好使用表單ID。

這樣,您可以刪除onsubmit屬性。

Replace your as per below
  <form class="form-horizontal" name="regist" action="Registration"
        onsubmit="return validateFile(this)" enctype="multipart/form-data"
        method="post">

        <div class="control-group" class="span12">
            <label class="control-label" for="file">Please upload a
                file:</label>
            <div class="controls">
                <input type="file" name="file"/>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <button type="submit" class="btn btn-primary">Upload</button>
            </div>
        </div>
    </form>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM