DIV CSS 佈局教程網

Bootstrap 下拉選單切換表單:點 A 顯示文字框、點 B 顯示圖片上傳
編輯:關於JavaScript     

需求是這樣的:頁面上有一個文字輸入框與一個圖片上傳框,文字框預設顯示、圖片上傳框預設隱藏。當使用者從下拉選單選 A 時顯示文字框、隱藏圖片上傳;選 B 時反之。

直覺的寫法如下,但實際操作會發現點擊完全沒有反應:

 <script type="text/javascript">
//image_upload是圖片上傳框的id,最開始時把它隱藏
$(document).ready( function(){
    $("#image_upload").hide();
});

//text_only是下拉菜單項,點擊時顯示 text_input,隱藏 image_upload
$('#text_only').click(function(e){
    $("#text_input").show();
    $("#image_upload").hide();
    e.stopPropagation();
});

/* image_only是下拉菜單項,點擊時隱藏文本框,顯示圖片上傳框 */
$('image_only').click(function(){
    $("#text_input").hide();
    $("#image_upload").show();
});
</script>

問題出在最後兩行的選擇器:$('image_only') 少了一個 #。少了井號,jQuery 會把它當成「尋找 image_only 這個標籤名稱」,頁面上當然找不到任何元素,事件也就永遠綁不上去。第一個 $('​#text_only') 寫對了,所以看起來像只有圖像那邊失效;實際上只要把兩處都補上 # 即可:

$('#image_only').click(function(){
    $("#text_input").hide();
    $("#image_upload").show();
});

另一個常見的類似狀況是:元素由 AJAX 動態插入,頁面載入時並不存在,此時直接 click() 一樣綁不上,該改用事件委派:

$(document).on('click', '#image_only', function(){
    $("#text_input").hide();
    $("#image_upload").show();
});

以下是修正後的完整程式碼:

 <!DOCTYPE html>
<html lang="en">
<head>
<title> hello,world</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--防止中文亂碼 -->
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-fileupload.css" rel="stylesheet" media="screen">
<script src="https://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-fileupload.js"></script>
<style>
.center {
    width : auto;
    display : table;
    margin-top:150px;
    margin-left: auto;
    margin-right: auto;
}
.text-center {
    margin-top:30px;
    display:table;
    margin-left:auto;
    margin-right:auto;
}
body{
    margin:0;
    background: url('img/955.jpg');
    background-size: 1440px 800px;
    background-repeat: no-repeat;
    display: compact;
    background-color: transparent;
}
.btn-large{ padding: 14px 34px; }
</style>
<script type="text/javascript">
$(document).ready( function(){
    $("#image_upload").hide();
});

$('#text_only').click(function(e){
    $("#text_input").show();
    $("#image_upload").hide();
    e.stopPropagation();
});

$('#image_only').click(function(){
    $("#text_input").hide();
    $("#image_upload").show();
});

$('#supervised').click(function(){
    $("#text_input").show();
    $("#image_upload").show();
});
</script>
</head>
<body>
<h1>hello,world</h1>
<div class="container">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li class="active"><a href="#">首頁</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"> 單模態 <b class="caret"></b> </a>
<ul class="dropdown-menu">
<li><a id="text_only" href="#">文本 </a></li>
<li><a id="image_only" href="#">圖像 </a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"> 多模態 <b class="caret"></b> </a>
<ul class="dropdown-menu">
<li><a id="supervised" href="#">有監督 </a></li>
<li><a id="unsupervised" href="#">無監督 </a></li>
</ul>
</li>
</ul>
</div>
</div><!-- end of navbar -->
<div class="center">
<form class="form-vertical">
<fieldset>
<input id="text_input" type="text" class="input-xxlarge search-query" placeholder="Text input">
</br>
<div id="image_upload" class="fileupload fileupload-new text-center" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input span3">
<i class="icon-file fileupload-exists"></i>
<span class="fileupload-preview"></span>
</div>
<span class=" btn btn-file">
<span class="fileupload-new">Select file</span>
<span class="fileupload-exists">Change</span>
<input type="file" />
</span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
<button type="submit" class="btn text-center btn-large btn-success"> Search </button>
</br>
</fieldset>
</form>
</div>
</div>
</body>
</html>

補充一個實務上的建議:這類「依選項切換輸入方式」的介面,最好把被隱藏欄位的 name 屬性一併移除或停用(用 disabled),否則表單送出時仍會帶著空白欄位。另外,若使用 Bootstrap 4 以上的版本,navbar 的結構與 class 名稱已大幅調整(navbar-inner、caret 等都不再需要),這份程式碼需要相應改寫才能正常運作。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved