我正在使用jQuery构建表单,当用户单击“添加更多”时,该表单会动态添加具有递增ID的选择字段。我需要能够根据用户先前在表单中选择的内容来显示或隐藏某些选项。
当用户单击单选按钮组中的“ A”时,“ Div A1”将被隐藏。当用户单击“ B”时,“ Div B1”将被隐藏,但是将显示“ Div A1”,依此类推。
这样做的最佳方法是什么?
$(function() {
$('.show_hide_div_list').hide();
$('#hide_div_A').click(function() {
$('.show_hide_div_list').show();
});
$('#hide_div_B').click(function() {
$('.show_hide_div_list').show();
});
$('#hide_div_C').click(function() {
$('.show_hide_div_list').show();
});
$('#hide_div_D').click(function() {
$('.show_hide_div_list').show();
});
var show_hide_div_max_fields = 36;
var show_hide_div_x = 0;
$('#show_hide_div_add').click(function(e) {
e.preventDefault();
if (show_hide_div_x < show_hide_div_max_fields) {
show_hide_div_x++;
var inps = $('#show_hide_div_wrapper >div:last').data('count') + 1 || 1;
$('#show_hide_div_wrapper').append('<div class="show_hide_div" data-count="' + inps + '"><select id="show_hide_div"><option value="div_a" id="div_a' + inps + '">Div A' + inps + '</option><option value="div_b' + inps + '" id="div_b' + inps + '">Div B' + inps + '</option><option value="div_c' + inps + '" id="div_c' + inps + '">Div C' + inps + '</option><option value="div_d' + inps + '" id="div_d' + inps + '">Div D' + inps + '</option></select><a class=remove>Remove</a><br><br></div>');
}
$('#show_hide_div_wrapper').on('click', 'a.remove', function() {
var inps = $('#show_hide_div_wrapper > div:last').data('count') - 1 || 1;
show_hide_div_x--;
$(this).closest('div').remove();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr class="show_hide_div">
<td style="vertical-align:top;">
Select which options you want to hide
</td>
<td>
<label>A</label>
<input type="radio" name="hide_div" id="hide_div_A" value="A">
<br>
<label>B</label>
<input type="radio" name="hide_div" id="hide_div_B" value="B">
<br>
<label>C</label>
<input type="radio" name="hide_div" id="hide_div_C" value="C">
<br>
<label>D</label>
<input type="radio" name="hide_div" id="hide_div_D" value="D">
</td>
</tr>
<tr class="show_hide_div_list" style="line-height:1em;">
<td>
</td>
<td>
<span id="show_hide_div_wrapper">
</span>
</td>
</tr>
<tr class="show_hide_div_list">
<td>
</td>
<td>
<button id="show_hide_div_add">Add More</button>
</td>
</tr>
</table>
$(function() {
$('.show_hide_div_list').hide();
var divArray = ['A', 'B', 'C', 'D']
divArray.forEach(function (item) {
$('#hide_div_' + item).click(function () {
$('.show_hide_div_list').show()
})
});
var show_hide_div_max_fields = 36;
var show_hide_div_x = 0;
$('#show_hide_div_add').click(function(e) {
e.preventDefault();
var optionArray = ['A', 'B', 'C', 'D'];
optionArray.forEach(function (item) {
if ($('#hide_div_' + item).prop('checked')) {
if (show_hide_div_x < show_hide_div_max_fields) {
show_hide_div_x++;
var inps = $('#show_hide_div_wrapper >div:last').data('count') + 1 || 1;
var div = $('<div>').addClass('show_hide_div').prop('data-count', inps);
var select = $('<select>').prop('id', 'show_hide_div');
optionArray.forEach(function (_item) {
if (_item != item) {
var option = $('<option>').prop('id', 'div_' + _item + inps).text('Div ' + _item);
select.append(option);
}
})
div.append(select).append('<a class=remove>Remove</a><br><br>');
$('#show_hide_div_wrapper').append(div);
}
}
});
$('#show_hide_div_wrapper').on('click', 'a.remove', function() {
var inps = $('#show_hide_div_wrapper > div:last').data('count') - 1 || 1;
show_hide_div_x--;
$(this).closest('div').remove();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="show_hide_div">
<td style="vertical-align:top;">
Select which options you want to hide
</td>
<td>
<label>A</label>
<input type="radio" name="hide_div" id="hide_div_A" value="A">
<br>
<label>B</label>
<input type="radio" name="hide_div" id="hide_div_B" value="B">
<br>
<label>C</label>
<input type="radio" name="hide_div" id="hide_div_C" value="C">
<br>
<label>D</label>
<input type="radio" name="hide_div" id="hide_div_D" value="D">
</td>
</tr>
<tr class="show_hide_div_list" style="line-height:1em;">
<td>
</td>
<td>
<span id="show_hide_div_wrapper">
</span>
</td>
</tr>
<tr class="show_hide_div_list">
<td>
</td>
<td>
<button id="show_hide_div_add">Add More</button>
</td>
</tr>
</table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句