【问题标题】:Html Select OptionHtml 选择选项
【发布时间】:2012-12-28 19:26:28
【问题描述】:

我有一个带有选项的列表框 itemValue 是一个字符串,并且它的显示顺序不准确。

我在 $(document).ready(function(){$("#item").val(1) 上使用并将item的索引位置设置为列表中的第一个。默认设置为到基于字母顺序的列表中的一个选项,这不是我想要的。

使用 JQuery 设置我想要的初始值索引有效,但是当我发布页面时,如果返回页面时页面上有任何错误,列表中项目的值将返回到我在document.ready() 函数。我怎么能克服这个。

代码;

jquery

$(document).ready(function(){

        $("#skinColorId").val(1);
});

html:

数据来自数据库

<li>
<form:label for="skinColorId" path="skinColorId">Select Skin Color</form:label>     
<form:select path="skinColorId" id="skinColorId" title="Select Skin Color">
<form:options items = "${skinColor.skinColorList}" itemValue="colorCode" itemLabel="colorDesc"/>
</form:select>          
<form:errors path="skinColorId" class="errors"/><label class="colorPreviewer" id="skinColorPreviewer">color previewer</label>
</li>

数据示例:

colorId|colorDesc|colorCode 155||选择颜色||#FFFFFF 156||水色||#00FFFF 157||海蓝宝石||#7FFFD4 158||天蓝色||#F0FFFF 159||米色||#F5F5DC 160||素瓷||#FFE4C4 161||黑色||#000000 162||白杏仁||#FFEBCD

【问题讨论】:

  • 您能否提供您的代码,以便为您提供帮助..
  • 请向我们展示一些标记和代码,以便我们为您提供帮助。也许设置一个小提琴。

标签: jquery html spring-mvc


【解决方案1】:

尝试使用“selected”设置默认值。

例如:

<option value="value" selected="selected">choose one</option>

发布后...检查服务器的响应。

if (response.success) {
  //clear the items value by using ( $('#item').html('');})
else{
  //if response if failed... or some error occured
  //then have the same value for your option tag
}

你真的不需要检查 $(document).ready 也不需要在 jquery 中设置值...

试试吧...希望它有效:)...

【讨论】:

    猜你喜欢
    • 2012-09-28
    • 2011-11-07
    • 2014-10-17
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多