gpt4 book ai didi

asp.net - 将 "onclick"属性添加到 asp.net 下拉列表项

转载 作者:数据小太阳 更新时间:2023-10-29 04:28:13 26 4
gpt4 key购买 nike

我可以像这样向 RadioButtonList 项目中的项目添加属性:

PaymentMethodDropDownList.Items[0].Attributes.Add("onclick", "javascript:showNoMethods();");
PaymentMethodDropDownList.Items[1].Attributes.Add("onclick", "javascript:showCreditCardMethod();");
PaymentMethodDropDownList.Items[2].Attributes.Add("onclick", "javascript:showSendPaymentMethod();");

但是,当我尝试将属性添加到 DropDownList 控件时,它似乎不起作用。我希望它是相似的。

最佳答案

这不能像 radioButtonList 那样完成,对于下拉列表,正确的属性事件名称是“onchange”而不是“onclick”。该事件应附加到 DropDownList 本身,而不是以下项目:

PaymentMethodDropDownList.Attributes.Add("onchange",
"showCreditCardMethod();");

此外,这有点复杂,需要自定义 javascript 函数来根据所选选项执行不同的操作。这是一个例子:

PaymentMethodDropDownList.Attributes.Add("onchange",
"handleDropDownEvents(this);");

自定义 Javascript 函数:这假定下拉项的值为“CreditCard”和“SendPayment”。

<script type="text/javascript">
function handleDropDownEvents(e){
if(e.value == "CreditCard"){
showCreditCardMethod();
}
else if(e.value == "SendPayment"){
showSendPaymentMethod();
}
}
</script>

关于asp.net - 将 "onclick"属性添加到 asp.net 下拉列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/821341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com