我有這個奇怪的問題,通過jQuery隱藏/顯示錶的行和下拉列表。我只想隱藏與下拉列表中選擇的內容不相關的數據,而不是基於過濾器回傳數據。這裏的片段:通過下拉列表隱藏行
$().ready(function() {
$("#SupplierID").change(changeme);
$("#BusinessID").change(changeme);
});
function changeme() {
$("table tr").show();
$("table tr td:nth-child(2):not(:contains('" + $("#SupplierID :selected").text() + "'))").parent().hide();
$("table tr td:nth-child(6):not(:contains('" + $("#BusinessID :selected").text() + "'))").parent().hide();
return false;
}
它的問題是,供應商ID工作正常,但BusinessID只有一次一個供應商ID已被選中的作品,而不是之前。如果我進入頁面並從下拉列表中直接選擇BusinessID,則不會發生任何事情。我有首先更改供應商ID下拉列表爲了它的工作。此外,如果將SupplierID設置爲「全部」選項(空白值),則BusinessID下拉列表不起作用。
任何想法?
編輯
事實上,這是因爲一個沒有價值的事情錯誤。事情是,這是我現在必須擁有的:
$(function() {
$("#SupplierID, #BusinessID").change(changeme);
});
function changeme() {
$("table tr").show();
if ($("#SupplierID :selected").val()) {
$("table tr td:nth-child(2):not(:contains('" + $("#SupplierID :selected").text() + "'))").parent().hide();
}
if ($("#BusinessID :selected").val()) {
$("table tr td:nth-child(6):not(:contains('" + $("#BusinessID :selected").text() + "'))").parent().hide();
}
}
這似乎有點討厭。任何關於如何使這更優雅的建議?
雖然我會如何捕捉錯誤?文本是「(全部)」,所以它基本上不會找到隱藏的行。我可以檢查所選的是否在索引0?這樣我知道這是全部選項。 – Kezzer