Javascript 一个函数如何使用来自另一个函数的变量?
我有一个函数Javascript 一个函数如何使用来自另一个函数的变量?,javascript,jquery,variables,jquery-selectors,Javascript,Jquery,Variables,Jquery Selectors,我有一个函数addText(),它有3个变量(textInput,type,rowID)。我声明它们时没有使用var关键字(这样它们就可以在函数之外使用了?)。我用这种方式创建了许多复选框: <td><input type="checkbox" name="CB" id="monitor_'+rowID+'"/></td> 此函数将在此处调用: $('#status_table #monitor_'
addText()
,它有3个变量(textInput,type,rowID
)。我声明它们时没有使用var
关键字(这样它们就可以在函数之外使用了?)。我用这种方式创建了许多复选框:
<td><input type="checkbox" name="CB" id="monitor_'+rowID+'"/></td>
此函数将在此处调用:
$('#status_table #monitor_'+rowID).each(function(rowID, textInput, type){
monitoring(rowID, textInput, type);
});
注意:inputText
是发布到某处的变量
问题:
$('#状态#表#监视器"+rowID)
$('#status_table#monitor#+rowID)。每个(function(){})
更改为$('#status_table tr')。每个(function(){})
addText()
函数(我在这个函数中包含了函数监视,而不是在上面的另一个jQuery操作中):
函数addText(添加类型、fb类型){
$(“#”+添加类型+“添加”)。单击(函数(){
$('.TextInput').empty();
textInput=$(“#”+fb_类型+“textInput”).val();
如果(textInput.length==0){
警报(“请输入字段名”);
返回;
}
index=$('#status_table tbody tr').last().index()+1;
type=$(“#select-choice-1”).find(“:selected”).text();
rowID=类型+文本输入;
var str=''+''+索引+''+行ID+
''+类型+''+
文本输入+“”+“”+
“x”+
'';
if(alreadyExist(textInput,键入)){
警报('条目存在。请输入另一个号码')
}否则{
$('status#u table tr:last')。在(str)之后;
}
监视(rowID、textInput、type);
返回;
});
}
表格的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>
输出状态
指数
行ID
反馈型
反馈号
地位
班长
去除
有几个问题。首先,这是行不通的:
$('#status_table #monitor_'+rowID).each(function(rowID, textInput, type){
monitoring(rowID, textInput, type);
});
传递给.each()
的函数有两个参数:indexInArray
和valueOfElement
,尽管您可以随意调用它们-在这种情况下,rowID
将是索引,textInput
将是元素的值<代码>类型将是未定义的,因为不会向其传递任何值
其次,在调用ID选择器后使用.each()
,没有任何意义。ID选择器最多只能选择一个元素,因为ID必须是唯一的
如果调用该.each()
时,rowID
、textInput
和type
都在范围内,那么您可以直接调用监控
,而根本不需要.each()
。所以只要:
monitoring(rowID, textInput, type);
这是使用变量调用选择器的正确方法吗
选择器只是字符串。用变量组合选择器就像用变量组合字符串一样
我是否应该在第二组代码中传递两次变量(在选择器函数和监视函数中)
取决于变量的作用域。如果变量与函数在同一范围内,则函数可以访问它。如果函数在变量所在的外部声明,则应将其传递给它们
如何正确传递变量
作为调用期间的参数,例如someFunction(theFoo、theBar、theOtherBar)
选择器正确吗?或者我应该将$('#status_table#monitor#'+rowID).each(function(){})更改为$('#status_table tr')。each(function(){})
ID在页面中应该是唯一的,并且只存在一次。因此,作为选择器的id应该足够了,比如$(“#monitor_u[someID]”)
。但这只意味着每次为每个rowID
获取一个。最好使用$('#status_table tr')。每个(function(){})
,并在tr
上循环
函数返回的参数,则不需要这样做。每个函数返回的参数都可以像$('#status_table#monitor"+rowID)一样执行代码>
但是,我要注意,。每个回调中的参数都是指定的indexInArray
和valueOfElement
,并且没有rowID
、number
或type
作为传递回的参数
myAwesomeFunction(myFirstVariable,mySecondVariable)
$('#status_table#monitor#+rowID)。每个(function(){})
更改为$('#status_table tr')。每个(function(){})
好吧,这真的取决于
$('#status_table #monitor_'+rowID).each(function(rowID, textInput, type){
monitoring(rowID, textInput, type);
});
monitoring(rowID, textInput, type);