简体   繁体   English

用javascript数组填充struts2选择标签

[英]Fill struts2 select tag with javascript array

Is it possible to fill the Struts 2 select tag with javascript array on load?是否可以在加载时用 javascript 数组填充 Struts 2 选择标签?

I have a code我有一个代码

<s:select id="attrStpSel" name="attrStpSel" headerKey="-1"
          headerValue="Select" list="${attributeActions}"
          labelposition="left" onchange="displaySelectAction()"
          cssStyle="width: 300px;" />

This is the script in the page.这是页面中的脚本。

<script type="text/javascript">
    $(document).ready(function() {
        var attributeActions = [ 'Add', 'Update', 'Search' ];
    });
</script>

Since it is a hard coded values, I don't want them to be populated from action.由于它是一个硬编码值,我不希望它们从动作中填充。 with the above code the javascript variable attributeActions is not being picked up.使用上面的代码,javascript 变量attributeActions没有被拾取。

What should be the ideal way to populate the static list into the Struts 2 select tag?将静态列表填充到 Struts 2 选择标签中的理想方式应该是什么?

You can create an inline list with #{} :您可以使用#{}创建内联列表:

<s:select name = "attrStpSel" 
     headerKey = "-1"
   headerValue = "Select" 
          list = "#{'Add','Update','Search'}" />

You can also create an inline map with the same notation:您还可以使用相同的表示法创建内联地图:

<s:select name = "attrStpSel" 
     headerKey = "-1"
   headerValue = "Select" 
          list = "#{'0':'Add' , '1':'Update' , '2':'Search'}" />

Struts select tag renders ordinary html select . Struts select 标签呈现普通的 html select It also adds additional tags, attributes, CSS to the generated html because it's UI tag.它还向生成的 html 添加了额外的标签、属性、CSS,因为它是 UI 标签。 You can read more about select tag.您可以阅读有关select标签的更多信息。

Render an HTML input tag of type select.呈现一个选择类型的 HTML 输入标签。

If you want to populate this select tag with javascript you can do something like this如果你想用 javascript 填充这个选择标签,你可以做这样的事情

$(function() {
    var attributeActions = [ 'Add', 'Update', 'Search' ];
    var $select = $('#attrStpSel');
    $select.find('option').remove();
    $('<option>').val("-1").text("Select").appendTo($select);
    $.each(attributeActions, function(index, value) {
      $('<option>').val(value).text(value).appendTo($select);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM