Search Function in Javascript
I have to develop a search function in javascript such that it will display a message/pop-up window when users does not select an option in dropdownlist. I am using usercontrols in my project. so the dropdownlist's are in .ascx file and my search function will be in .aspx file. Here is the code which I am using:
function Search()
{
var src_status = createObj("bodyuc_drp_Status").value;
var src_program = createObj("bodyuc_drp_program").value;
if(document.getElementById(src_program).value == 0 && document.getElementById(src_client).value == 0)
{
alert("Please select atleast one client or program")
return false;
}
else {
createObj("hdn_search").value = "Search";
开发者_运维技巧 return true;
}
}
the value '0' in the if condition is the index of '--select one--' option in the dropdownlist. The above alert message should be displayed when the index is 0 otherwise the user should get the data based on his selection.
This code gives me alert message even if the users select a value other than the 'select one' option. Can anyone tell my why?
really appreciate your help.
The value
property of the select
element is the same as the value
property for the selected option
element, not its index. To get the index, use the selectedIndex
property:
if(document.getElementById(src_program).selectedIndex == 0 && document.getElementById(src_client).selectedIndex == 0)
精彩评论