【问题标题】:How to convert an html form into ajax request for a rest controller if one of the fields multi select?如果其中一个字段多选,如何将 html 表单转换为休息控制器的 ajax 请求?
【发布时间】:2020-12-31 22:23:11
【问题描述】:

问题是字段“roles”是一个多选的选择列表,因此必须将字段转换为数组。我找不到有关如何执行此操作的任何信息。需要帮忙。 我正在这样做:

$.ajax({
            url: '/api/users',
            async: true,
            dataType: 'json',
            contentType: "application/json",
            type: "PUT",
            data: {
                id : jQuery('#id').val(),
                username: jQuery('#username').val(),
                password: jQuery('#password').val(),
                roles :
            },
        })

【问题讨论】:

    标签: jquery json ajax rest


    【解决方案1】:

    只要您使用标准的多个<select> 标签,jQuery val() 就已经是一个字符串数组。您可以将其指定为与您的用户名和密码值完全相同。

    <select id="roles" multiple>
      <option>Admin</option>
      <option>Editor</option>
      <option>User</option>
    </select>
    

    data: {
        id : jQuery('#id').val(),
        username: jQuery('#username').val(),
        password: jQuery('#password').val(),
        roles : jQuery('#roles').val()
    },
    

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多