1

クローンを作成して onchange を実行した後、select on change のクラスと値を取得したいと思いますが、元の select の値のみを返します。以下の私のコードを見つけてください

<!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>
4

2 に答える 2

0

$-operatorDOM で最初に出現し、たまたまオリジナルです。

最も簡単な解決策は、クローン要素をDOM構造のオリジナルの前に配置することです。

.beforeを使用してそれを行うことができます

別の解決策は、複製された要素の ID/名前を変更し、ID/名前で値を取得することです。

("#clonedElement").attr("id","myID");
于 2013-06-12T07:53:16.277 に答える