admin管理员组文章数量:1355559
I can dynamically append "select" control to DOM,after appending it to DOM,I wanna change the html content of the last "select"(the latest "select" added dynamically),but it failed...
(I cannot set options value in param_html,becuase I should set them by using ajax request later.)
<script>
$(function(){
var param_html = '<select class="params"></select>';
$("input[value='+']").click(function(){
$('#parameters').append(param_html);
$('.params :last').html('<option>aaa</option><option>keyword in profile</option><option>last tweet</option>');
});
});
</script>
<div id="parameters">
<input type="button" value="+">
<select class="params"><option>1</option><option>2</option></select>
</div>
any suggestion is appreciated.
I can dynamically append "select" control to DOM,after appending it to DOM,I wanna change the html content of the last "select"(the latest "select" added dynamically),but it failed...
(I cannot set options value in param_html,becuase I should set them by using ajax request later.)
<script>
$(function(){
var param_html = '<select class="params"></select>';
$("input[value='+']").click(function(){
$('#parameters').append(param_html);
$('.params :last').html('<option>aaa</option><option>keyword in profile</option><option>last tweet</option>');
});
});
</script>
<div id="parameters">
<input type="button" value="+">
<select class="params"><option>1</option><option>2</option></select>
</div>
any suggestion is appreciated.
Share Improve this question asked Jun 17, 2011 at 14:45 Mark MaMark Ma 1,3623 gold badges19 silver badges35 bronze badges 3- This is very confusing question, and the script you've provided doesn't seem to make sense in context. Can you try and clarify? – Jivings Commented Jun 17, 2011 at 14:48
- 1 actually, it's a problem in his selector ;) – Patricia Commented Jun 17, 2011 at 14:49
- sorry,my english is poor,I have tried my best to express what I want to say correctly – Mark Ma Commented Jun 17, 2011 at 15:17
3 Answers
Reset to default 6take out the space between params and :last
$('.params:last').html('<option>aaa</option><option>keyword in profile</option><option>last tweet</option>');
your content seems to be added after the dom has been loaded.
try live http://api.jquery./live/
Well your CSS selector is imporper should be $('.params:last-child')
or $('.params:last')
i think the space bar is not allowed there.
Also no one forbis You from using the object you've created:
$(function(){
var param_html = '<select class="params"></select>';
$("input[value='+']").click(function(){
$('#parameters').append(param_html);
$(param_html).html('<option>aaa</option><option>keyword in profile</option><option>last tweet</option>');
});
});
If you are going to use AJAX in future then the same idea will work instead of reselecting the object use to one you've created.
本文标签: javascriptjquery selector doesn39t work when dynamically append htmlStack Overflow
版权声明:本文标题:javascript - jquery selector doesn't work when dynamically append html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743973934a2570800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论