Javascript 限制JQuery中的附录数量
我不想在每次单击按钮时限制Jquery中发生的追加次数Javascript 限制JQuery中的附录数量,javascript,jquery,ajax,Javascript,Jquery,Ajax,我不想在每次单击按钮时限制Jquery中发生的追加次数 function standardRoom() { var flag = 0 if ($('select#selectBoxStandard option').length > 1 ) { flag++ $('#selectBoxStandard').find("option:nth-last-child(-n+" + $('#selectBoxStandard').val() + ")").remov
function standardRoom() {
var flag = 0
if ($('select#selectBoxStandard option').length > 1 ) {
flag++
$('#selectBoxStandard').find("option:nth-last-child(-n+" + $('#selectBoxStandard').val() + ")").remove();
if (flag <= 1) {
$("#roomDetail ul").append('<li><strong>Standard Room - Regular Online Rate</strong> </li>').append('<li class="pull-right"><h4 style="color:darkorange">PHP {{$availableRooms[0]['nightRate']}}</h4></li>').append('<li>Number of night(s): {{$n_nights}} </li>').append('<li>Number of person(s): </li>').append('<li class="hr">Number of room(s): </li>').append(flag);
}
}else {
alert("No more rooms");
}
}
功能标准间(){
var标志=0
如果($('select#selectBoxStandard option')。长度>1){
旗++
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+”++$('#selectBoxStandard')。val()+”)。删除();
如果(flag每次调用standardRoom函数时,flag
变量重置为0,因此它将始终添加元素。请确保将该变量存储在更全局的位置,而不需要重置它:
var flag = 0;
function standardRoom() {
if ($('select#selectBoxStandard option').length > 1 ) {
flag++
$('#selectBoxStandard').find("option:nth-last-child(-n+" +
$('#selectBoxStandard').val() + ")").remove();
if (flag <= 1) {
$("#roomDetail ul").append('<li><strong>Standard Room - Regular Online Rate</strong> </li>').append('<li class="pull-right"><h4 style="color:darkorange">PHP {{$availableRooms[0]['nightRate']}}</h4></li>').append('<li>Number of night(s): {{$n_nights}} </li>').append('<li>Number of person(s): </li>').append('<li class="hr">Number of room(s): </li>').append(flag);
}
} else {
alert("No more rooms");
}
}
var标志=0;
功能标准间(){
如果($('select#selectBoxStandard option')。长度>1){
旗++
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+)”+
$(“#selectBoxStandard”).val()+”).remove();
如果(flag每次调用standardRoom函数时,flag
变量重置为0,因此它将始终添加元素。请确保将该变量存储在更全局的位置,而不需要重置它:
var flag = 0;
function standardRoom() {
if ($('select#selectBoxStandard option').length > 1 ) {
flag++
$('#selectBoxStandard').find("option:nth-last-child(-n+" +
$('#selectBoxStandard').val() + ")").remove();
if (flag <= 1) {
$("#roomDetail ul").append('<li><strong>Standard Room - Regular Online Rate</strong> </li>').append('<li class="pull-right"><h4 style="color:darkorange">PHP {{$availableRooms[0]['nightRate']}}</h4></li>').append('<li>Number of night(s): {{$n_nights}} </li>').append('<li>Number of person(s): </li>').append('<li class="hr">Number of room(s): </li>').append(flag);
}
} else {
alert("No more rooms");
}
}
var标志=0;
功能标准间(){
如果($('select#selectBoxStandard option')。长度>1){
旗++
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+)”+
$(“#selectBoxStandard”).val()+”).remove();
如果(flagflag
在每次函数调用时重置。请在函数外部定义它
var flag = 0
function standardRoom() {
if ($('select#selectBoxStandard option').length > 1 ) {
flag++
$('#selectBoxStandard').find("option:nth-last-child(-n+" + $('#selectBoxStandard').val() + ")").remove();
if (flag <= 1) {
$("#roomDetail ul").append('<li><strong>Standard Room - Regular Online Rate</strong> </li>').append('<li class="pull-right"><h4 style="color:darkorange">PHP {{$availableRooms[0]['nightRate']}}</h4></li>').append('<li>Number of night(s): {{$n_nights}} </li>').append('<li>Number of person(s): </li>').append('<li class="hr">Number of room(s): </li>').append(flag);
}
}else {
alert("No more rooms");
}
}
var标志=0
功能标准间(){
如果($('select#selectBoxStandard option')。长度>1){
旗++
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+”++$('#selectBoxStandard')。val()+”)。删除();
如果(flagflag
在每次函数调用时重置。请在函数外部定义它
var flag = 0
function standardRoom() {
if ($('select#selectBoxStandard option').length > 1 ) {
flag++
$('#selectBoxStandard').find("option:nth-last-child(-n+" + $('#selectBoxStandard').val() + ")").remove();
if (flag <= 1) {
$("#roomDetail ul").append('<li><strong>Standard Room - Regular Online Rate</strong> </li>').append('<li class="pull-right"><h4 style="color:darkorange">PHP {{$availableRooms[0]['nightRate']}}</h4></li>').append('<li>Number of night(s): {{$n_nights}} </li>').append('<li>Number of person(s): </li>').append('<li class="hr">Number of room(s): </li>').append(flag);
}
}else {
alert("No more rooms");
}
}
var标志=0
功能标准间(){
如果($('select#selectBoxStandard option')。长度>1){
旗++
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+”++$('#selectBoxStandard')。val()+”)。删除();
如果(flag,则可以使用代替局部变量。
在dom就绪状态下,您可以为selectBoxStandard元素设置flag data属性的值,然后使用它:
//初始化标志值
$('selectBoxStandard')。数据('flag',0);
功能标准间(){
//获取标志值
var flag=$('#selectBoxStandard')。数据('flag');
如果($('select#selectBoxStandard option')。长度>1){
//更新标志值
$('#selectBoxStandard')。数据('flag',flag++)
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+”++$('#selectBoxStandard')。val()+”)。删除();
如果(flag,则可以使用代替局部变量。
在dom就绪状态下,您可以为selectBoxStandard元素设置flag data属性的值,然后使用它:
//初始化标志值
$('selectBoxStandard')。数据('flag',0);
功能标准间(){
//获取标志值
var flag=$('#selectBoxStandard')。数据('flag');
如果($('select#selectBoxStandard option')。长度>1){
//更新标志值
$('#selectBoxStandard')。数据('flag',flag++)
$('#selectBoxStandard')。查找(“选项:第n个最后的子项(-n+”++$('#selectBoxStandard')。val()+”)。删除();
如果(标记您的标记
变量在每次调用函数时都重置为零。您将变量定义为0,向其中添加1,然后检查它是否等于1。提示:它总是会。您的标记
变量在每次调用函数时都重置为零。您将变量定义为0,向其添加1,然后检查它是否等于1。提示:它是永远都会。是的…我看到了我的坏蛋是的…我看到了我的坏蛋