如何以编程方式设置 jQuery 下拉检查列表的值?

发布于 2024-10-20 05:48:41 字数 338 浏览 1 评论 0原文

如何以编程方式将某些复选框设置为选中状态?

场景如下: 一旦我通过管理屏幕创建用户,我就会有几个 jqueryDropdown 复选框,一个用于“用户角色”,一个用于“用户区域”,因为用户可以拥有与他/她关联的多个角色和多个区域。没问题,我已经完成并保存到数据库中。 我还为用户提供了更新用户角色和区域的能力,并且我想通过 jQuery 下拉检查列表来完成此操作,因此我的方法如下:

  1. 为他们提供用于创建用户的相同屏幕
  2. 发出 Ajax 请求获取用户特定的角色和区域。
  3. 现在我想做的是基于 Ajax 响应集,通过 DOM 脚本检查与响应值匹配的复选框。有人知道该怎么做吗?

How can I programatically set some checkboxes to selected?

The scenario is as follows:
Once I create a user through the admin screen I have a couple of jqueryDropdown checkboxes one for "User Roles" and one for "User Regions" since a user can have many roles and many regions associated with him/her. No problem there I got that accomplished and saved to the database.
I also give the user the ability to update the user roles and regions and I want to do it through the jQuery dropdown-check-list so my approach is as follows:

  1. Give them the same screen they used to create the user
  2. Issue an Ajax request to get the user specific roles and regions.
  3. Now what I want to do is based on the Ajax response set to checked the checkboxes that match the value of the response through DOM Scripting. Does anybody know how to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎樣才叫好 2024-10-27 05:48:41

我已经找到了我自己问题的答案。首先我想提一下,我想填充这个 jquery 组件 http: //dropdown-check-list.googlecode.com/svn/trunk/doc/dropdownchecklist.html 如果我原来的问题有误导性,我们深表歉意。

填充我的 jquery 下拉清单插件的 Javascript 如下:

$(document).ready(function() {

    //These will apply the jquery drop down checklist to both of selects
    $(".s1").dropdownchecklist({ width: 205});
    $( "button, input:button", null).button();
    loadUserRoles();
    loadUserRegions();
});


function loadUserRoles(){

    var userName = $("#userName").val();
    var hostname = getHostName();
var requestURL = hostname.concat("fos", "/users/userRoles?userName="+userName);

    $.getJSON(requestURL, null, function(data){
        var userRoles = new Array();

        $.each(data.list, function(i,item){
            userRoles.push(item.id.toString());
        });

        $("#role").dropdownchecklist("destroy");
        $("#role").val(userRoles);
        $("#role").dropdownchecklist();
        //$("#role").dropdownchecklist("refresh");
});
}

function loadUserRegions(){

    var userName = $("#userName").val();
    var hostname = getHostName();
var requestURL = hostname.concat("fos", "/users/userRegions?userName="+userName);

    $.getJSON(requestURL, null, function(data){
        var userRegions = new Array();

        $.each(data.list, function(i,item){
            userRegions.push(item.id.toString());
        });

         $("#region").dropdownchecklist("destroy");
         $("#region").val(userRegions);
         $("#region").dropdownchecklist();
});
}

function getHostName(){

var url = document.URL;
var rv = url.indexOf("fos");
var hostname = url.substring(0, rv);
return hostname;
}

返回 JSON 的 grails 操作如下

def userRoles = {

  /* This action gets the user roles and returns it as JSON*/
    def user = Users.findByUserName(params["userName"])
    def rolesInstanceList = Authorities.findAllByUser(user)

    def outputList = new JSONObject()
    def rolesList = new JSONArray()
    def jsonRole = null

    rolesInstanceList.each {
      jsonRole = new JSONObject()
      def role = it.role
      jsonRole.put("id", role.id)
      jsonRole.put("roleName", role.roleName)
      rolesList.put(jsonRole)
    }
    outputList.put("list", rolesList)
    render outputList as JSON      
}

I have found the answer to my own problem. First I want to mention that I wanted to populate this jquery component http://dropdown-check-list.googlecode.com/svn/trunk/doc/dropdownchecklist.html sorry if my original question was missleading.

The Javascript to populate my jquery drop down checklist plugin is as follows:

$(document).ready(function() {

    //These will apply the jquery drop down checklist to both of selects
    $(".s1").dropdownchecklist({ width: 205});
    $( "button, input:button", null).button();
    loadUserRoles();
    loadUserRegions();
});


function loadUserRoles(){

    var userName = $("#userName").val();
    var hostname = getHostName();
var requestURL = hostname.concat("fos", "/users/userRoles?userName="+userName);

    $.getJSON(requestURL, null, function(data){
        var userRoles = new Array();

        $.each(data.list, function(i,item){
            userRoles.push(item.id.toString());
        });

        $("#role").dropdownchecklist("destroy");
        $("#role").val(userRoles);
        $("#role").dropdownchecklist();
        //$("#role").dropdownchecklist("refresh");
});
}

function loadUserRegions(){

    var userName = $("#userName").val();
    var hostname = getHostName();
var requestURL = hostname.concat("fos", "/users/userRegions?userName="+userName);

    $.getJSON(requestURL, null, function(data){
        var userRegions = new Array();

        $.each(data.list, function(i,item){
            userRegions.push(item.id.toString());
        });

         $("#region").dropdownchecklist("destroy");
         $("#region").val(userRegions);
         $("#region").dropdownchecklist();
});
}

function getHostName(){

var url = document.URL;
var rv = url.indexOf("fos");
var hostname = url.substring(0, rv);
return hostname;
}

My grails action that returns JSON is as Follows

def userRoles = {

  /* This action gets the user roles and returns it as JSON*/
    def user = Users.findByUserName(params["userName"])
    def rolesInstanceList = Authorities.findAllByUser(user)

    def outputList = new JSONObject()
    def rolesList = new JSONArray()
    def jsonRole = null

    rolesInstanceList.each {
      jsonRole = new JSONObject()
      def role = it.role
      jsonRole.put("id", role.id)
      jsonRole.put("roleName", role.roleName)
      rolesList.put(jsonRole)
    }
    outputList.put("list", rolesList)
    render outputList as JSON      
}
眉黛浅 2024-10-27 05:48:41

我创建了一个类结构,其中一般创建了 DDCL,并且代码:

$('#selectorid').dropdownchecklist('destroy');

破坏了最初创建的 DDCL 的完整性。

我发现这个解决方案更适合我的应用程序:

$('#selectorid').val(values);
$('#selectorid').dropdownchecklist('refresh');

其中“值”是值数组(例如 [1, 2, 3])。这不会破坏最初创建的 DDCL 的完整性。

I've created a class structure, where a DDCL is created generically and the code:

$('#selectorid').dropdownchecklist('destroy');

destroys the integrity of the DDCL that was initially created.

I found that this solution works much better for my application:

$('#selectorid').val(values);
$('#selectorid').dropdownchecklist('refresh');

where 'values' is an array of values (e.g. [1, 2, 3]). This does not destroy the integrity of the DDCL initially created.

夜巴黎 2024-10-27 05:48:41

返回一些带有状态的 JSON...

{
  "roles": {
    "role1": true,
    "role2": false
  },
  ...
}

然后一些 jQuery 应该做到这一点...

$.each(json.roles, function(role, state) {

   $(':checkbox[name="' + role + '"]')[0].checked = state;

});

Return some JSON with the state...

{
  "roles": {
    "role1": true,
    "role2": false
  },
  ...
}

Then some jQuery should do it...

$.each(json.roles, function(role, state) {

   $(':checkbox[name="' + role + '"]')[0].checked = state;

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文