Javascript 动态选择命名元素
我有这个密码Javascript 动态选择命名元素,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我有这个密码 $('#addPhone').click(function() { phoneCount = $("#phoneTabs").tabs("length") + 1; $('#phoneTabs').show(); $('#phoneTabs').append('<div id="phoneTabs' + phoneCount + '"><form id="phoneForm' + phoneCount + '" novalidate="no
$('#addPhone').click(function() {
phoneCount = $("#phoneTabs").tabs("length") + 1;
$('#phoneTabs').show();
$('#phoneTabs').append('<div id="phoneTabs' + phoneCount + '"><form id="phoneForm' + phoneCount + '" novalidate="novalidate" action="" method="post"><table><tr><td><b>Phone Number ' + phoneCount + '</b></td><td></td></tr><tr><td>Phone number</td><td><input type="text" class="required digits" maxlength="10" minlength="10" name="phone_number' + phoneCount + '" /></td></tr><tr><td>Comment</td><td><textarea rows="5" cols="25" name="phone_comment' + phoneCount + '"></textarea></td></tr></table><br /><button id="addPhone' + phoneCount + '">Add</button></form></div>');
$("#phoneForm" + phoneCount).validate({
submitHandler: function() {
return false;
}
});
$('#addPhone' + phoneCount).button();
$('#addPhone' + phoneCount).click(function() {
$(this).button({
disabled: true
});
$('#phoneForm' + phoneCount + ' input').attr('disabled', true);
});
$("#phoneTabs").tabs("add", "#phoneTabs" + phoneCount, phoneCount);
$('#phoneTabs').tabs("select", phoneCount - 1);
phoneCount++;
});
为什么当我点击addPhone按钮时,只有按钮被禁用,为什么作为phoneForm“+phoneCount”子元素的其他输入元素没有被禁用
我做错了什么?如果更改行,可能是禁用最后一个选项卡上的输入
$(this).button({disabled : true});
$('#phoneForm'+ phoneCount+ ' input').attr('disabled',true);
将来
应该希望得到激活的表单。这是一个,phoneCount值在应用于单击处理程序之前发生更改,要删除闭包,请尝试以下操作
$('#addPhone' + phoneCount).click((function(selector){ return function() {
$(this).button({
disabled: true
});
$(selector).attr('disabled', true);
}})('#phoneForm' + phoneCount + ' input'));
编辑
如果您想在函数中使用phoneCount的值,那么只将其传递给IIFE
$('#addPhone' + phoneCount).click((function(phoneCount){ return function() {
if (phoneCount operator operand){
code
}
$(this).button({
disabled: true
});
$('#phoneForm' + phoneCount + ' input, other selectors').prop('disabled', true);
}})(phoneCount));
最好使用.propdisabled,true或.attrdisabled,disabled。查看其余代码可能会有所帮助。@Skystar3您应该真正提高您的代码格式化技能。.propdisabled,true或.attrdisabled,disabled--尝试了这两种方法都不起作用这是有效的语句吗$selector.attr'disabled',true;}'phoneForm'+phoneCount+'input';是的,它是一个立即调用的函数表达式IIFE。它从函数选择器{Firebug:missing;before statement}箭头这里的'phoneForm'+phoneCount+'input'开始;它工作了,但只禁用了phoneForm“+下的第一个输入元素phoneCount@Skystar3代码中只有一个输入元素,按钮和文本区域不是输入
$('#addPhone' + phoneCount).click((function(phoneCount){ return function() {
if (phoneCount operator operand){
code
}
$(this).button({
disabled: true
});
$('#phoneForm' + phoneCount + ' input, other selectors').prop('disabled', true);
}})(phoneCount));