回发后重置下拉到初始值?

时间:2015-07-02 19:48:01

标签: javascript jquery asp.net forms

如果在aspx表单中有一个id为ddlSomething的下拉列表,单击重置按钮(<input type='reset'...)会将所有下拉列表重置为索引0.如何将下拉列表还原为所选索引在用户开始使用表单进行修改之前,我立即使用javascript将所有值重置为空并将所有下拉索引重置为0?

请注意,我不是在问如何重置表单。我知道怎么做。

我不是问如何使用jquery或javsacript将所选索引设置为0.我知道如何做到这一点。

我希望在回发后立即将下拉列表重置为选中的索引,并使用jquery将所有值重置为默认值(文本框中没有任何内容,下拉列表中选择的索引为0)。

踢腿和咯咯笑的示例代码:

...
<select id='ddlSomething'>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select> 
<input type="button" value="ClearValues" onclick="ClearForm();" />
<input type="reset" value="Reset Values" id="resetAll" class="ResetButton" />
...
...
    function ClearForm() {
        //handle text
        jQuery("input[type='text'],input[type='number'],input[type='email'],input[type='tel']").each(function () {
            jQuery(this).val("");
        });
        jQuery("option:selected").each(function () {
            jQuery(this).removeAttr('selected');
        });
        jQuery("select option:first-child").each(function () {
            jQuery(this).attr("selected", "selected");
        });
    }

1 个答案:

答案 0 :(得分:1)

我会在页面中创建一个隐藏字段,并将其值设置为回发期间下拉列表的选定索引。然后在JS代码中,您可以引用它,读取它的值并在下拉列表中设置索引。也许不是最干净的解决方案,但它应该能够胜任。

相关问题