我有一個功能addText()
,它有3個變量(textInput, type, rowID
)。我宣佈他們沒有var
關鍵字(這樣它們可以在函數之外使用?)。我有我創造了這樣多的複選框:函數如何使用來自其他函數的變量?
<td><input type="checkbox" name="CB" id="monitor_'+rowID+'"/></td>
然後,我有這個功能,這需要使用這3個變量:
function monitoring(rowID, number, type) {
var $check = $('#status_table #monitor_' + rowID);
$('#test').append($check);
if($check.is(':checked')) {
$.post('/request', {
inputText: number,
key_pressed: type
}).done(function (reply) {
if(reply == "on") {
$('#test').append("on");
} else {
$('#test').append("off");
}
});
}
return;
}
該功能將在這裏稱爲:
$('#status_table #monitor_'+rowID).each(function(rowID, textInput, type){
monitoring(rowID, textInput, type);
});
注意:inputText
是發佈到某處的變量。
問題:
這是正確的方法來調用與變量選擇?
$('#status_table #monitor_'+rowID)
我應該通過變量兩次(在選擇器功能和監測功能兩者),第二組的碼?
如何正確傳遞變量?
選擇器是否正確?或者我應該將
$('#status_table #monitor_'+rowID).each(function(){})
更改爲$('#status_table tr').each(function(){})
?
5. ADDED:在哪裏以及如何調用函數「monitoring」?我把它放在addText下面(參考下面的代碼),但它沒有任何意義,因爲只有當點擊「添加」按鈕時纔會執行函數。此外,由於我的選擇器是一個變量,我想確保它可以對所有選中的複選框執行相同的操作。我怎樣才能做到這一點?
我試過了,但當我檢查複選框時,我的代碼根本沒有任何反應。
編輯:
這是我addText()
功能(我已經包含在這個函數,而不是如上另一個jQuery的作用下功能監測):
function addText(add_type, fb_type) {
$("#" + add_type + "Add").click(function() {
$('.TextInput').empty();
textInput = $("#" + fb_type + "TextInput").val();
if(textInput.length === 0) {
alert('please enter the fieldname');
return;
}
index = $('#status_table tbody tr').last().index() + 1;
type = $("#select-choice-1").find(":selected").text();
rowID = type + textInput;
var str = '<tr id="' + rowID + '">' + '<td>' + index + '</td><td>' + rowID +
'</td><td class="type_row_' + textInput + '">' + type + '</td><td class="feedback number">' +
textInput + '</td>' + '<td><img src="static/OffLamp-icon.png" class="image" id="off"></td>' +
'<td><input type="checkbox" name="CB" id="monitor_' + rowID +
'" class="custom" data-mini="true" /><label for="CB"> </label></td><td class="outputRemove">x</td>' +
'</tr>';
if(alreadyExist(textInput, type)) {
alert('Entry exists. Please enter another number.')
} else {
$('#status_table tr:last').after(str);
}
monitoring(rowID, textInput, type);
return;
});
}
表的HTML:
<table class="config" id="status_table">
<thead>
<tr>
<th colspan="4" ; style="padding-bottom: 20px; color:#6666FF; text-align:left; font-size: 1.5em">Output Status</th>
</tr>
<tr>
<th>Index</th>
<th>Row ID</th>
<th>Feedback Type</th>
<th>Feedback Number</th>
<th>Status</th>
<th>Monitor?</th>
<th>Remove?</th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
你能最終確定你的問題嗎?似乎你有一些錯誤的名字和東西被遺漏了。 – Joseph 2013-04-29 08:45:48
你是指哪一個?我可以在必要時添加更多細節。 – yvonnezoe 2013-04-29 08:47:54
我們可以看到表格的HTML嗎? – 2013-04-29 08:49:19