【发布时间】:2013-04-22 20:58:47
【问题描述】:
我有一个函数 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.补充:我应该在哪里以及如何调用函数“监控”?我把它放在 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>
【问题讨论】:
-
你能先完成你的问题吗?好像你的名字写错了,有些东西漏掉了。
-
你指的是哪一个?我可以在必要时添加更多详细信息。
-
我们可以看看表格的 HTML 吗?
-
请阅读
.each()提供的参数。 api.jquery.com/each -
@yvonnezoe 发布围绕
.each()调用的代码会有所帮助;如果这是在addText()函数中,则将其全部发布(或将其剥离到相关部分)。
标签: javascript jquery variables jquery-selectors