我想讓頁面專注於一個空元素,如果它在一個摺疊的div內,它將打開該元素並專注於所需的空字段。我正在使用引導來實現崩潰功能。需要HTML5,打開摺疊和焦點所需的元素如果爲空
此刻,如果表單試圖提交,並且有一個空的必填字段,它不會提交,但沒有什麼可以說明原因。
我不確定如何打開摺疊的元素,並在表單嘗試提交時如果爲空則關注所需的字段。
片段中的JavaScript用於JQuery UI手風琴,其中正試圖讓它適用於引導程序摺疊功能。
下面是HTML & CSS
// save the accordion in a variable as you'll need it later
$collaspe = $("#accordion");
// when the submit is clicked
$("#formwrite input[type='submit']").on("click", function(event) {
// traverse all the required elements looking for an empty one
$("#formwrite input[required='required']").each(function() {
// if the value is empty, that means that is invalid
if ($(this).val() == "") {
// find the index of the closest h3 (divide by 2 because jQuery UI accordion goes in pairs h3-div. A bit hacky, sorry)
var item = $(this).closest(".ui-accordion-content").prev().index()/2;
// open that accordion section that contains the required field
$collaspe.accordion("option", "active", item);
// stop scrolling through the required elements
return false;
}
});
});
.container {
width: 75%;
}
.panel-heading {
background-color: #D9DBDE;
padding: 20px;
margin-bottom: 10px;
border-radius: 0;
}
.panel-title {
font-size: 21px;
display: block;
width: 100%;
color: #4F5858;
line-height: 1.3;
font-weight: normal;
}
.collapse-icon {
float: right;
}
.fieldpos {
margin-left: 0px;
width: 60%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<body>
<form action="/update" id="formwrite">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title va-middle">Account Settings
\t \t \t \t \t \t <img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeOne" aria-expanded="true" aria-controls="collaspeOne">
\t \t \t \t \t \t </h4>
</div>
<div id="collaspeOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="panel panel-default">
<div class="row fieldpos">
<fieldset class="form-group textwide">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter Email" required>
</fieldset>
</div>
</div>
</div>
</div>
</div>
<div class="panel">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title va-middle">Other Settings
\t \t \t \t \t \t \t \t <img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeTwo" aria-expanded="true" aria-controls="collaspeTwo">
\t \t \t \t \t \t \t \t </h4>
</div>
<div id="collaspeTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel panel-default">
<div class="row fieldpos">
<fieldset class="form-group textwide">
<label for="group">Test</label>
<input type="text" class="form-control" id="group" placeholder="test" required>
</fieldset>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-success form-control savechanges">Save Changes</button>
</form>
</body>
我如何能做到這一點的任何建議,我看過像我自己,如this where I have tried the JS in my own其他幾個問題,但已能夠根據需要進行工作。
你是否已經對'javascript'做了一些編碼?發佈它 – Vikrant
其他'.panel'元素內有'.panel'元素。這是一個錯誤嗎? – Evochrome
@Vikrant現在發佈,我試圖使用我自己鏈接的其他問題的代碼,但不斷收到錯誤嘗試,至少得到我需要的東西。 – Studento919