我学了这么久,你告诉我循环添加选项是这么搞的?

博客介绍添加元素分为手动与自动添加,需依项目需求选择。重点阐述限定条件自动循环添加选项,以游乐场成人、儿童、婴儿下拉框为例,说明各下拉框添加规则,成人用for循环,儿童依成人改变,婴儿依儿童改变,最终实现成人+儿童+婴儿<10。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发工具与关键技术:VisualStudio C#
作者:落白
撰写时间:2019/06/17

对于添加元素分为手动添加与有条件的自动添加。相对而言,手动添加的操作性更强,自动添加则更为准确和省力。但无论是手动添加还是自动添加都是开具体情况来使用的,主要开项目的需求。
下面要说介绍的是限定条件自动循环添加选项。举个例子,下面有三个下拉框,分别是成人、儿童、婴儿。而游乐场有限定,一个成人只能带一个儿童或者婴儿,儿童和婴儿不能单独买票。而且成人、儿童、婴儿加起来的数量不能大于9,大于9时就要购买团队票。接下来我们就需要根据这个限定条件来实现循环添加选项。
在这里插入图片描述
成人的下拉框选项直接用for循环来直接添加,因为它受到的约束条件只是小于9。儿童下拉框则根据成人下拉框的改变事件来添加,婴儿下拉框则根据儿童事件的改变事件来添加。最终实现的效果是成人+儿童+婴儿<10
在这里插入图片描述
功能代码:

//循环添加成人下拉框的选项
        for (var i = 1; i <=9; i++) {
$("#Select").append("<option onclick='AdultNumber(this)'>" + i + "</option>");
        }
        //成人下拉框改变事件
        $("#Select").change(function () {
            var AdultS = $("#Select").val();
            var Childerns = 9 - AdultS;
            $("#children").empty();
            //循环添加成人下拉框的选项
            for (var j = 0; j <= Childerns; j++) {
                $("#children").append("<option>" + j + "</option>");
            }
        });
        //儿童下拉框改变事件
        $("#children").change(function () {
            var AdultS = $("#Select").val();
            var Childrens = $("#children").val();
            var Babys = 9 - AdultS - Childrens;
            $("#baby").empty();
            //循环添加成人下拉框的选项
            for (var K = 0; K <= Babys; K++) {
                $("#baby").append("<option>" + K + "</option>");
            }
        });
用法举例:从数据表中name字段写入VALComboBox的items,id字段写入VALComboBox的values,当从VALComboBox选择一个选项后,就可以从value属性获得相应的ID值,或者写value的值为某ID值,VALComboBox将定位在相应的选项上;unit VALComboBox;interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Menus, Dialogs, StdCtrls;type TValComboBox = class(TComboBox) private FValue: PString; FValues: TStrings; FOnChange: TNotifyEvent; function GetValue: string; function GetButtonValue(Index: Integer): string; procedure SetValue(const Value: string); procedure SetValues(Value: TStrings); protected procedure Change; dynamic; procedure Notification(AComponent: TComponent; Operation: TOperation); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property Value: string read GetValue write SetValue; property ItemIndex; published property Values: TStrings read FValues write SetValues; property OnChange: TNotifyEvent read FOnChange write FOnChange; end;procedure Register;implementationconstructor TValComboBox.Create(AOwner: TComponent);begin inherited Create(AOwner); FValue := NullStr; FValues := TStringList.Create; style := csDropDownList;end;destructor TValComboBox.Destroy;begin DisposeStr (FValue); FValues.Free; inherited Destroy;end;procedure TValComboBox.Notification(AComponent: TComponent; Operation: TOperation);begin inherited Notification(AComponent, Operation);end;function TValComboBox.GetValue : string;begin result:=values[itemindex];end;function TValComboBox.GetButtonValue(Index: Integer): string;begin if (Index < FValues.Count) and (FValues[Index] ‘‘) then Result := FValues[Index] else if (Index < Items.Count) then Result := Items[Index] else Result := ‘‘;end;procedure TValComboBox.SetValue (const Value: string);var I : Integer;begin AssignStr(FValue, Value); if (ItemIndex < 0) or (GetButtonValue(ItemIndex) Value) then begin if (ItemIndex >= 0) then ItemIndex := -1; for I := 0 to Items.Count - 1 do begin if GetButtonValue(I) = Value then begin ItemIndex := I; break; end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值