I am populating a dropdown with dynamic values and on change of that dropdown I am calling another function. Now if only one value comes for the drop开发者_Python百科down I want to show it as a static text and at the the same time call the function. Any suggestions ?
If you provide your code I will be able to give a more customized answer, but here goes:
1: When you fetch the dynamic data for the dropdownlist, use a count based if / else
1.1 : If the number of values fetched is 1 (one) then 1.1.1 : Hide the dropdownlist and inset a literal in it's position 1.1.2 : Call the second function
1.2 : If more than 1 (one) values is feteched then 1.2.1 : Populate the dropdownlist with the values 1.2.2 : Bind the .change() function to the dropdownlist
Sample Code:
$(document).ready(function(){
$.ajax(
url: 'asdasd',
type: POST,
success: function(data){
var p = [];
$.each(data, function(key, val){
p.push(key, val);
});
if(p.length > 1)
{
// bind values to dropdownlist and add handle for change event
}
else
{
$(".dropdownlist").html(p[0][1]);
fetchSecondaryData();
}
}
);
});
if i understand you correctly you're filling a select dynamically and binding a function to its change event, and if the value is 'MyValue' run a special code. you can add this change to your function something like:
$('select.yourSelectClass).change(function(){
if $(this).val() == 'YourValue')
{
// Do whatever you have to do when this value is selected
}
// General processing for the change event
});
精彩评论