加载选项以使用jquery选择 [英] loading options to select using jquery

查看:64
本文介绍了加载选项以使用jquery选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JqueryAjax来加载Select元素中的选项.

I am using JqueryAjax to load options in Select element.

<div>
    <select id="roleType" name="roleType" 
             onclick="loadRoleTypes();">
               <option value="s">Select</option>
     </select>
</div>

JqueryAjax

JqueryAjax

function loadRoleTypes()
{           
    $('#roleType').empty().append('<option>select</option>');        
    $.ajax({
            url: '/ci/ajaxcall/loadRoles.php',
            dataType: 'json',
            type: 'POST',
            data: {"userCode": userCode},
            success: function(response) {
              var array = response.value;
              if (array != '')
              {
                for (i in array) {                        
                 $("#roleType").append("<option>"+array[i].ROLE_TYPE+"</option>");
               }

              }

            },
            error: function(x, e) {

            }

        });
}

在这里,当我单击选择元素时,我得到的选项值为下拉.但是我无法选择特定的选项.如何解决此问题

here i am getting option values as drop down when i click select element.but i cant select a particular option.how to solve this

推荐答案

您无法选择选项的问题是,每当您单击选择框(包括选项集)时,都会调用名为loadRoleTypes()的函数.您可以尝试检查选择框中的选项数量,也可以检查其他任何条件.

The problem you can't select an option is that every time you click the selectbox(Including the option set) the function called loadRoleTypes() is being called.You can try checking the number of options in your selectbox.Or you could check on any other conditions.

function loadRoleTypes()
{           
if ($('#roleType').find("option").size() == 1) {  //Check condition here
    $('#roleType').empty().append('<option>select</option>');        
    $.ajax({
            url: '/ci/ajaxcall/loadRoles.php',
            dataType: 'json',
            type: 'POST',
            data: {"userCode": userCode},
            success: function(response) {
              var array = response.value;
              if (array != '')
              {
                for (i in array) {                        
                 $("#roleType").append("<option>"+array[i].ROLE_TYPE+"</option>");
               }

              }

            },
            error: function(x, e) {

            }

        });
}
}

这篇关于加载选项以使用jquery选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆