[英]In a table each row has an edit button. Show or hide the button depends on another value in this row
我在js文件中定義了一個表,最后一列是每一行的編輯按鈕,以允許用戶編輯該行的信息。 還有另一行,第三行,MlsCode。 僅當MlsCode的值為“ A”或“ B”時,我們才應在此行中顯示編輯按鈕。 以下是表構建器的一部分:
var agentSearchResultBuilder =
[
{ colName: "First Name", dataName: "FirstName" },
{ colName: "Last Name", dataName: "LastName" },
{ colName: "MLS", dataName: "MlsCode" },
{
colName: "MLS Office Status",
dataName: "Office.MlsIsActive",
template: '{{if Office.MlsIsActive}}Active{{else}}Inactive{{/if}}',
styleClass: '{{if Office.MlsIsActive}}active{{else}}inactive{{/if}}'
},
{
colName: "Edit",
template: "<button title='Edit Agent Information' class='tinyButton editAgent' id='${MlsCode}'>Edit</button>"
}
];
我編寫了以下代碼來控制編輯按鈕的可見性,但是只有第一個匹配的行可以顯示該按鈕。 后來的匹配行未顯示該按鈕。 我不知道怎么了
$("button.editAgent", table).button().hide(); //to hide all buttons initially
$('#agentSearchTable >tbody >tr').each(function () {
var value = $(this).find('td:eq(2)').text();
if (value === "A") {
$("#A").show();
alert(value);
/*I use this to test if it reaches every
matching row or not. Turns out yes. Each matching row
gives alert. But edit button didn't show(only first
matching one shows.)
*/
}
if (value === "B") {
$("#B").show();
alert(value);
}
});
另外,我在agentSearchResultBuilder中注意到,有一些類似{if} .. {/ if}的代碼,所以我想知道是否有一種方法可以在編輯按鈕的模板中簡單地做到這一點。 就像id等於“ A”或“ B”一樣,顯示按鈕。 我對C#,JavaScript和jQuery完全陌生。 這是我的第一個項目。 希望您能幫助我,非常感謝!
這個:
$("#A").show();
無法正常工作,因為這取決於在多行按鈕上具有相同的ID,這不是合法的HTML。 瀏覽器可能會忽略已經具有的id的id
屬性。 您可能想使用一個類,而不是一個ID(在class
屬性的末尾添加button-${MLSCode}
並刪除id
屬性),並使用如下代碼:
$(this).find('.button-a').show();
展示它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.