0

jQuery は初めてで、問題が発生しています。わかりやすくするために単純化します。

3 つのタブがあり、そのすべてに同じ ID のボタンと同じ ID のラベルがあるとします。ユーザーがタブ 1 のボタンをクリックすると、タブ 1 のラベルから .html() を参照したい。ユーザーがタブ 2 のボタンをクリックすると、タブ 2 のラベルから .html() を参照したいなどなど

これを取得する簡単な方法はありますか、それともラベル/ボタンごとに一意の ID を取得する必要がありますか?

前もって感謝します。

編集: コードを追加すると、既存のタブを含む私の index.jsp は次のようになります。

    <div id="tabs">
<ul>
<li>
<a href="${pageContext.request.contextPath}/your_searches" id="tab1"><span>My Searches</span></a>
</li>
<li>
<a href="${pageContext.request.contextPath}/public_searches" id="tab2"><span>Public Searches</span></a>
</li>
<li>
<a href="${pageContext.request.contextPath}/new_search" id="tab3"><span>New Search</span></a>
</li>

<li>
<a href="#tabs-4"  id="tab4">View Article</a>
</li>

</ul>

<div id="tabs-1"></div>
<div id="tabs-2"></div>
<div id="tabs-3"></div>
<div id="tabs-4"> <%@include file="view_article2.jsp"%></div>

</div>

<div id="dialog"                     title="Alert!"></div>
<div id="editDialog"                 title="Alert!"></div>
<div id="editDialogYourSearch"       title="Alert!"></div>
<div id="updateSearchCriteriaDialog" title="Alert!"></div>

存在する jQuery はかなり複雑ですが、これを使用してクリック イベントにタブを追加します。

$("#" + tabId).load($("#context").val() + '/resources/templates/searchResultsTemplate.html', function () {

var html = $("#" + tabId).html();
$("#" + tabId).html(html.replace(/#tabId#/g, tabId));

var headlnTrEnabled = data.searchResults.searchVO.headlnTrEnabled;
var docTrEnabled    = data.searchResults.searchVO.docTrEnabled;
var languageName    = data.searchResults.searchVO.languageName;

if(headlnTrEnabled == 0) {
        document.getElementById("translated-" + tabId ).disabled=true;
        $("label[for='translated-" + tabId + "']").text("Language translation is unavailable for '" + languageName + "'");
}

// Process the click event on the 'translated' checkbox
processTranslationSupportedCheckboxEvents( tabId );


//alert("new_search queryAndPopulateArticlesSearchResults: before populateResultsPageSearchCriteria");
// Set Search Criteria value in Search Results' hidden fields
populateResultsPageSearchCriteria( data.searchResults, tabId );

// Initialize DataTable
initDataTable( '#articleTable-' + tabId );

// Define double-click method on data rows
initArticleTableDoubleClick( '#articleTable-',
                                                         tabId,
                                                         data.searchResults.searchVO.languageCode,
                                                         "#error" );

// Populate Search Results UI from article list
// var counter = populateSearchResultsTable( tabId, data.searchResults.articleList );
checkHighlight();

$('#searchInfo').html( data.hash );

これらの追加されたタブ (searchResultsTemplate.html) では、ボタン クリックからラベルを参照する必要があります。これまでに私が持っている単純化された HTML は次のとおりです。

<input type="checkbox" id="translated-#tabId#"/><label for="translated-#tabId#">Translate Article Titles</label>

<div align="right"><input type="button" value="Export Excel CSV File.." id="exp-csv" />
<br>
<label id="DownloadLink"></label>
</div>

<input id = "btnSubmit" type="submit" value="Release"/>

<p id="searchTitle-#tabId#" name="searchTitle"></p>

<label id="searchInfo">None</label>

#exp-csv ボタンは、現在選択されているタブの #searchInfo ラベルを参照する必要があります。

4

2 に答える 2

0

HTML での目標が何であるかはわかりませんが、次のようなことを意味していると思います。

$(".tab").children("button").on("click", function() {
   var genericVariable = $(this).siblings("label").html();
});

これは兄弟要素 (隣接する要素) でのみ機能することに注意してください。ラベル タグがボタンとは別の別のタグ内にある場合は、最初に、たとえばparent().eq(-1)を使用してタグ ツリーを上に移動し、次にラベルのあるタグまで下に移動する必要があります。

于 2013-10-29T13:40:40.020 に答える