如何将所选项目从下拉列表传递给Javascript函数? [英] How to pass the selected items from dropdownlist to a Javascript function ?
问题描述
亲爱的,
我需要使用 OnClientClick 将选择的项目从 DropDownList 传递给Javascript函数,以在botton受到对方欢迎时对其进行验证.
这是我的代码,不适用于我,任何人都可以告诉我问题出在哪里:
Dears ,
I need to pass the selected item from a DropDownList to a Javascript function using OnClientClick to validate it when the botton is cliked on the cilent side.
here is my code that does not work with me , can any body tell me where is the issue:
<asp:Button ID="Save" runat="server" Text="Save" Width="173px"
onclick="_Run_Click" OnClientClick="return DateValidation('<%=yFrom.ClientID%>','<%=yTo.ClientID%>')" />
<script type="text/javascript">
function DateValidation(yFrom, yTo) {
var startYear = document.getElementById(yFrom);
var endYear = document.getElementById(yTo);
if (startYear != '' && endYear != '')
{
if (endYear < startYear)
{
alert('Start date cannot be greater than end date');
return false;
}
else { return true; }
}
}
</script>
谢谢,
Thanks,
推荐答案
var startYear = document.getElementById(yFrom);
这只是给您控制对象.听起来您想比较两个日期值.
试试:
This gave you just the control object. Sounds like you want to compare the two date value.
Try:
function DateValidation(yFrom, yTo) {
var startYear = document.getElementById(yFrom);
var endYear = document.getElementById(yTo);
if (startYear.value != '' && endYear.value != '')
{
if (endYear.value < startYear.value)
{
alert('Start date cannot be greater than end date');
return false;
}
else { return true; }
}
}
更新:
为了进行日期比较,您需要将文本框字符串转换为日期.
此处:
UPDATE:
For date comparison, you need to convert the textbox strings into dates.
Here:
function DateValidation(yFrom, yTo) {
var startYear = document.getElementById(yFrom);
var endYear = document.getElementById(yTo);
if (startYear.value != '' && endYear.value != '')
{
Date endDate = new Date(endYear.value);
Date startDate = new Date(startYear.value);
if (endDate < startDate)
{
alert('Start date cannot be greater than end date');
return false;
}
else { return true; }
}
}
确保文本框具有有效的Javascript日期,否则您将收到错误消息.
这篇关于如何将所选项目从下拉列表传递给Javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!