私は質問の作成で小さなアンケートを作成していますが、質問テキストと質問の選択肢があります。ダイナミックコントローラでダイナミックイベントを作成する方法
デフォルトでは、別のテキストボックスを追加するには3つのテキストボックスがあります。追加ボタンをクリックするか、最後のテキストボックスに別のテキストボックスを追加しますが、イベントは同じテキストボックスに貼り付けられますイベントを処理するための唯一の最後のテキストボックスが、私はしたいかわからない私のコードで間違っている:
はJQuery:
<script>
$(document).ready(function() {
$('.dropdown_selector').change(function() {
var option = $(this).find('option:selected').val();
if (option == "Radio Button") {
$(".QuestionOption").html('<p>Q1:<input id="Text1" type="text" /></p> <p> Answer Choices:</p> <p><input id="Text2" type="text" /></p><p><input id="Text3" type="text" /></p> <p class ="last4"><input id="Text4" type="text" /></p>');
$(".ButtonField").html('<p><input id="Button1" type="button" value="Save" /> <input id="Button2" type="button" value="Cancel" /></p><p><input id="Checkbox1" type="checkbox" />Add a Common Field</p>');
}
else
if (option == "Check Box") {
$(".QuestionOption").html('<p>Q1:<input id="Text1" type="text" /></p> <p> Answer Choices:</p> <p><input id="Text2" type="text" /></p><p><input id="Text3" type="text" /></p> <p class ="last4"><input id="Text4" type="text" /></p>');
$(".ButtonField").html('<p><input id="Button1" type="button" value="Save" /> <input id="Button2" type="button" value="Cancel" /></p><p><input id="Checkbox1" type="checkbox" />Add a Common Field</p>');
}
});
});
</script>
<script>
$(document).ready(function() {
var counter = 5;
var nb = counter - 1;
$(".QuestionOption").on("click", "p.last"+nb, function() {
if (counter > 10) {
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('p')).attr("class", 'last' + counter);
newTextBoxDiv.after().html('<input type="text" id="Text' + counter + '" value="" > ');
newTextBoxDiv.appendTo(".QuestionOption");
counter++;
nb = counter - 1;
});
$("#addButton").click(function() {
if (counter > 10) {
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('p')).attr("class", 'last' + counter);
newTextBoxDiv.after().html('<input type="text" id="Text' + counter + '" value="" > ');
newTextBoxDiv.appendTo(".QuestionOption");
counter++;
});
$("#removeButton").click(function() {
if (counter == 3) {
alert("No more textbox to remove");
return false;
}
counter--;
$(".last" + counter).remove();
$("#Text"+counter).remove();
});
});
HTMLコード:
ページのロードのみ、一度だけ実行されますImage of the question and choice
'$("。QuestionOption ")。on'は、ページが読み込まれると1回だけ実行されます。これは、起動時に(4、見た目で)「nb」の値を使用して、 'p.last" + nb'と一致する要素にリスナーを追加します。 'p.last4'と一致しません。 – ADyson
はい、それはテキストボックス番号4のためだけに添付されていますので、解決策は何ですか? –