我想在克隆并执行 onchange 后获取更改时选择的类和值,但它仅返回原始选择的值。请在下面找到我的代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Clone</title>
</head>
<body>
<div id="input1" class="clonedInput">
<select id="fields" class="select">
<option>Test One</option>
<option>Test Two</option>
<option>Test Three</option>
<option>Test Four</option>
</select>
</div>
<button>Clone</button>
</body>
</html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
var num = $('.clonedInput').length, // how many "duplicatable" input fields we currently have
newNum = new Number(num + 1), // the numeric ID of the new input field being added
newElem = $('#input' + num).clone().attr('id', 'input' + newNum).fadeIn('slow');
newElem.find('select#fields').attr('class','select' + newNum).val('');
$('#input' + num).after(newElem);
})
$('select').change(function(){
var value = $(this).val(),
classx = $(this).attr('class');
console.log(value + ' | ' + classx);
})
})
</script>