所以這是頁面的樣子目前:如何在爲動態創建的輸入選擇下拉選項時調用jquery函數?
第一種是在硬編碼,然後將其餘的添加/刪除的按鈕。第一個也可以添加或刪除按鈕。我想調用一個jQuery的功能,當下拉列表更改爲從文本框/單選按鈕(和文本)/複選框(和文本)等類型更改。
當前它只適用於第一個問題/答案,只適用於它是原始的而不是動態創建的。我不確定這是爲什麼。
這裏是如何創建的Q/A和刪除
$("#addButton").click(function() {
if (counter > max_fields) {
alert("Only " + max_fields + " Questions allowed");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Question #' + counter + ' : </label>' +
'<input type="text" name="textbox' + counter +
'" id="questionbox' + counter + '" value="" />' +
' <select id="choice'+ counter +'"><option>Type</option><option>Radio Button</option><option>Text Box</option><option>Check Box</option></select>' +
'<button id = "remove' + counter + '">Remove</button>' +
'<br/><label>Answer #' + counter + ' : </label>' +
'<div id="Answers' + counter + '">' +
'Option 1: <input type="text" id="answerbox' + counter +
'1" name="answerbox' + counter + '" value="" />' +
'<br/>Option 2: <input type="text" id="answerbox' + counter +
'2" name="answerbox' + counter + '" value="" />' +
'<br/>Option 3: <input type="text" id="answerbox' + counter +
'3" name="answerbox' + counter + '" value="" />' +
'<br/>Option 4: <input type="text" id="answerbox' + counter +
'4" name="answerbox' + counter + '" value="" /></div>');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function() {
if (counter == 1) {
alert("No more textbox to remove");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove();
});
這是我試圖得到它改變類型
$('#choice1').change(function() {
var selected_item = $(this).val()
var searchEles = document.getElementById("Answers1").children;
alert(searchEles.length);
for(var i = 0; i < searchEles.length; i++) {
$('#answerbox1' + i).attr('type', selected_item);
//alert(searchEles.length);
}
});
的網頁代碼如下
<input type='button' value='Add Question' id='addButton'/>
<input type='button' value='Remove Question' id='removeButton'/>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Question #1 : </label>
<input type='text' id='questionbox1'/>
<select id="choice1" onchange="$('#choice').val('id');"> //this on change was added and currently does nothing it seems.
<option value="">Type</option>
<option value="radio">Radio Button</option>
<option value="text">Text Box</option>
<option value="checkbox">Check Box</option>
</select>
<button id="remove1">Remove</button>
<br/><label>Answer #1 : </label>
<div id="Answers1">
Option 1: <input type="text" id='answerbox11' name='answerbox1' value="" />
<br/>Option 2: <input type="text" id='answerbox12' name='answerbox1' value="" />
<br/>Option 3: <input type="text" id='answerbox13' name='answerbox1' value="" />
<br/>Option 4: <input type="text" id='answerbox14' name='answerbox1' value="" />
</div>
</div>
</div>
我試圖做一些像onchange這樣的東西,然後得到ID並從那裏去,但沒有奏效。我知道它不匹配後端jQuery名稱。
TL; DR
- 我不知道如何動態寫jQuery函數工作 爲他們所有。
- 我不知道爲什麼即使我將其硬編碼到
#choice1
它將在第一次創建時工作 但是如果我刪除並添加它,即使它 具有相同的確切值,也不會。我認爲它可能可能與 for循環有關,因爲警報甚至不會觸發第二次 左右。
這是答案(假設你正在使用jQuery 1.7+),但你應該使用不是動態添加的.selector的最接近的父項(例如表單或包含div) – Craig
有沒有一種方法可以抽象這樣做更多,不必使用#choice1,而只是發送它的價值?我也是這麼說的。 $(document).on(「change」,「#choice1」,function(){ – Nick
您是否試圖通過$(this).attr(「value」)獲得所選選項的值? –