【问题标题】:How to make <html:select> tag REQUIRED如何使 <html:select> 标签成为必需的
【发布时间】:2025-12-03 00:20:19
【问题描述】:

在 HTML 中,select 标记具有 required 属性,该属性“指定用户需要在提交表单之前选择一个值”。 read here

在 Struts 1 中,“html:select”标签没有属性“required”:read here

在 Struts 1 中为选择框提交表单之前,如何指定用户需要选择一个值?

<td class="insdataheader">
    <label for="initReqList[0].seqType">
         <bean:message bundle="XYZ" key="it.myproject.cbi2.disposals.v000104.model.MndtInitiationRequestV02.seqType"/>
    </label>
</td>

<td class="insdatitxt_inserita_nowrap" align="left">
    <html:select property="initReqList[0].seqType" name="initReqList[0].seqType" styleId="initReqList[0].seqType" value="${dataEntryForm.initReqList[0].seqType}">
          <html-ext:keyOptionsCollection bundle="XYZ" name="dataEntryForm" property="initReqList[0].seqTypeOptions" label="keyBundle"value="key"  
   </html:select>
</td>

【问题讨论】:

    标签: html struts-1 taglib


    【解决方案1】:

    试试这样的,javascript

    JQuery

     $(function(){
                var elem = document.getElementsByName('initReqList[0].seqType')[0];
                jQuery(elem).attr('required','required');
            });
    

    如果在 htmt tld 中需要这个而不是你必须编辑 html tld 并且必须向它和相应的 java 类添加自定义代码

    【讨论】:

    • 太棒了!我还有一个问题,有没有办法让标签变成红色?如果我尝试放置
    • 其他类覆盖它并没有冲突。
    【解决方案2】:

    &lt;html:select /&gt;标签中添加required属性,你必须有一个空选项。

    例子:

    <html:select required="required" property="initReqList[0].seqType" name="initReqList[0].seqType" styleId="initReqList[0].seqType" value="${dataEntryForm.initReqList[0].seqType}">
        <html-ext:keyOptionsCollection bundle="XYZ" name="dataEntryForm" property="initReqList[0].seqTypeOptions" label="keyBundle"value="key"  
    </html:select>
    

    【讨论】:

    【解决方案3】:

    这对我有用 - 将第一个值设为空 - 要求对空值有效。

    <select required>
     <option value="">Please select</option>
     <option value="one">One</option>
     <option value="two">Two</option>
    </select>
    

    必需的属性是一个布尔属性。指定后,用户将需要在提交表单之前选择一个值。

    【讨论】:

    • 是的,在普通的 html 中它可以工作,但我怎样才能在带有 html:select 标记的 struts 中做到这一点?