这是我的html代码:歌厅相对选择元素的值aftered与jQuery
<input type="button" class="addSection" value="Add section" />
<br />
<hr />
<div id='rep'></div>
,这是我的脚本:
$(document).ready(function() {
$('.addSection').click(function() {
$('#rep').after('<br />' +
'<select class="srtCom">' +
'<option value="key">key</option>' +
'<option value="luck">luck</option>' +
'<option value="college">college</option>' +
'</select>' +
'<br />');
alert($('#rep').next('.srtCom').val());
});
});
我的问题是,为什么我不能得到aftered选择元素的值。 我使用next()函数来达到选择元素,并得到它的价值,但在alert()函数我得到了undefined! 感谢您的帮助。
这里的下一个元素是'br'。而不是你的'
@KingKing *“这里的下一个元素是br”* - 不是,它是'$('#rep')。next'。 –
@SpencerWieczorek自己检查https://jsfiddle.net/ruftftdx/ –