我有一个功能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