1 回答

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個贊
這是一種方法。- 我已將以下檢查添加到您的代碼中:
if ($('#total_sites').val() >= 5) { $('button:contains("Add Mentor")').prop('disabled', true); }
它的作用是檢查 div 中元素的值#total_sites
。如果該值大于或等于 5,它將禁用Add Mentor button
.
我在本例中禁用了該按鈕,但您可以通過執(zhí)行類似的檢查來執(zhí)行適合您偏好的操作。
編輯: 要回答評論中的問題,您可以通過設(shè)置計(jì)數(shù)來添加編號標(biāo)題,如下所示:
var count = $('#total_sites').val();
然后將計(jì)數(shù)包含在新元素中,如下所示:
var count = '<h1> Mentor' + count + '</h1>'
最后,像其他元素一樣附加它:
$('#new_site').append(count);
運(yùn)行下面的代碼片段以查看其實(shí)際效果。
function addSite() {
var new_total_sites = parseInt($('#total_sites').val()) + 1;
var count = new_total_sites
var count = '<h1> Mentor' + count + '</h1>'
if ($('#total_sites').val() >= 5) {
$('button:contains("Add Mentor")').prop('disabled', true);
}
var new_site_label = "<label id='new_label1_" + new_total_sites +
"'>Additional Research Participation: </label>";
var new_site_input = "<input id='new_site_" +
new_total_sites +
"' type='text' class='form-control' list='sites' name='site[]'></br>";
var new_slot_label = "<label id='new_label2_" + new_total_sites +
"'>Research Institution</label>";
var new_slot_input = "<input class='form-control' id='new_slot_" +
new_total_sites + "' name='slots[]'></br>";
var new_research_label = "<label id='new_label3_" + new_total_sites +
"'>Research Mentor</label>";
var new_research_input = "<input class='form-control' id='new_research_" +
new_total_sites + "' name='slots[]'></br>";
var new_form_group_label = "<label class= id='new_label4_" + new_total_sites +
"'>Type of Research</label>";
var new_basic_label = "<input type='checkbox' id='checkbox1" + new_total_sites + "' value='basic'><label for='basic' id='basic" + new_total_sites + "'>Basic</label></br>";
var new_clinical_label = "<input type='checkbox' id='checkbox2" + new_total_sites + "' value='clinical'><label for='clinical' id='clinical" + new_total_sites + "'>Clinical</label></br></div>";
$('#new_site').append(count);
$('#new_site').append(new_site_label);
$('#new_site').append(new_site_input);
$('#new_site').append(new_slot_label)
$('#new_site').append(new_slot_input);
$('#new_site').append(new_research_label);
$('#new_site').append(new_research_input);
$('#new_site').append(new_form_group_label);
$('#new_site').append(new_basic_label);
$('#new_site').append(new_clinical_label)
$('#total_sites').val(new_total_sites)
}
function removeSite() {
var last_total_site = $('#total_sites').val();
if (last_total_site > 1) {
$('#new_label1_' + last_total_site).remove('');
$('#new_site_' + last_total_site).remove('');
$('#new_label2_' + last_total_site).remove('');
$('#new_slot_' + last_total_site).remove('');
$('#new_label3_' + last_total_site).remove('');
$('#new_research_' + last_total_site).remove('');
$('#new_label4_' + last_total_site).remove('');
$('#basic' + last_total_site).remove('');
$('#clinical' + last_total_site).remove('');
$('#checkbox1' + last_total_site).remove('');
$('#checkbox2' + last_total_site).remove('');
$('#total_sites').val(last_total_site - 1);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id=new_site></div>
<button type="button" class="btn btn-default" onClick="addSite()">Add Mentor</button>
<button type="button" class="btn btn-default" onClick="removeSite()">Remove Mentor</button>
<input type="hidden" value="1" id="total_sites">
</br>
</br>
添加回答
舉報(bào)