當我想通過ajax調用添加新的複選框輸入時,我的腳本存在嚴重問題。問題是我插入後無法檢索此複選框的值。當我改變選擇1和選擇2和選擇3和選項。警報必須顯示每個字段更改的4個字段的值。Jquery .change無法檢索插入的複選框值
我這裏舉一個例子中的jsfiddle:https://jsfiddle.net/pm4t91ab/39/
腳本代碼:
<script>
$(function() {
$(".ch").change(function() {
// Récupérer les données du formulaire
var select1 = $("select#id1").val();
var select2 = $("select#id2").val();
var country = $("select#country").val();
var options = "";
var op = "<td>Options :</td>" +
"<br />" +
"<td>" +
"<div>" +
"<label>" +
"<input type='checkbox' class='ch' id='option1' name='options' class='ch' value='1'>Option1</label>" +
"</div>" +
"<div>" +
"<label>" +
"<input type='checkbox' id='option2' name='options' class='ch' value='2'>Option2</label>" +
"</div>" +
"</td>";
if (country != "") {
$('#options_insert').html(op);
}
// Bind the elements dynamically created here and get the selected items
$(document).on('click', 'input[name="options"]', function() {
var options = $("input[name=options]:checked").map(function() { return this.value;}).get().join(",");
});
alert("select1=" + select1 + " select2=" + select2 + " Country=" + country + " options=" + options);
});
});
</script>
HTML代碼:
<table>
<tr>
<td>
Selection 1 :
</td>
<td>
<select name="select1" class="ch" id="id1">
<option value="">Nothing</option>
<option value="val1">val1</option>
<option value="val2">val2</option>
<option value="val3">val3</option>
</select>
</td>
</tr>
<tr>
<td>
Selection 2 :
</td>
<td>
<select name="select2" class="ch" id="id2">
<option value="">Nothing</option>
<option value="val1">sel1</option>
<option value="val2">sel2</option>
<option value="val3">sel3</option>
</select>
</td>
</tr>
<tr>
<td>
Selection 3 :
</td>
<td>
<select name="select" class="ch" id="country">
<option value="">Nothing</option>
<option value="val1">Maroc</option>
<option value="val2">USA</option>
<option value="val3">Egypt</option>
</select>
</td>
</tr>
<tr id="options_insert">
</tr>
</table>
任何幫助將非常apreciated
我看到你的HTML沒有複選框;只選擇(下拉菜單)。你正在尋找添加一個監聽器到新的下拉菜單嗎? –
請訪問我的jsfiddle並更改第三選擇的值。或者在我的腳本中,var op包含複選框代碼 – Codinga