首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HiddenField值不会被删除

HiddenField值不会被删除
EN

Stack Overflow用户
提问于 2011-11-01 05:50:46
回答 3查看 428关注 0票数 0

我有一个javascript函数,它在2选中多个框之间移动项目,当我将项目从源选择框移到目标选择框时,我将该值添加到我的hiddenfield中,以便我可以在代码后面访问,工作良好,但是当我将项目从目标选择框移回源选择框时,我尝试使用: hidMemType.value = "";来清除隐藏字段。我认为这是可行的,但显然在回发的情况下,该项目仍然卡在目标框中。请指教,谢谢。

代码语言:javascript
复制
// Move items to and fro select box
        function move(sourceFrom, sourceTo) {

            var hidOutlet = document.getElementById('<%=hdnOutlet.ClientID%>');
            var hidMemType = document.getElementById('<%=hdnMemType.ClientID%>');
            var hidMemStatus = document.getElementById('<%=hdnMemStatus.ClientID%>');

            var arrFrom = new Array();
            var arrTo = new Array();
            var arrLU = new Array();
            var i;
            for (i = 0; i < sourceTo.options.length; i++) {
                arrLU[sourceTo.options[i].text] = sourceTo.options[i].value;
                arrTo[i] = sourceTo.options[i].text;
            }
            var fLength = 0;
            var tLength = arrTo.length;
            for (i = 0; i < sourceFrom.options.length; i++) {
                arrLU[sourceFrom.options[i].text] = sourceFrom.options[i].value;
                if (sourceFrom.options[i].selected && sourceFrom.options[i].value != "") {
                    arrTo[tLength] = sourceFrom.options[i].text;
                    tLength++;
                } else {
                    arrFrom[fLength] = sourceFrom.options[i].text;
                    fLength++;
                }
            }

            sourceFrom.length = 0;
            sourceTo.length = 0;
            var ii;

            for(ii = 0; ii < arrFrom.length; ii++) 
            {
            var no = new Option();
            no.value = arrLU[arrFrom[ii]];
            no.text = arrFrom[ii];
            sourceFrom[ii] = no; // SENDS VALUE FROM DESTINATION BOX BACK TO SOURCE BOX
            hidMemType.value = ""; // TRY TO CLEAR MY HIDDEN FIELD HERE
            }


            for (ii = 0; ii < arrTo.length; ii++) {
                var no = new Option();
                no.value = arrLU[arrTo[ii]];
                no.text = arrTo[ii];
                //sourceTo.options.add(no);
                sourceTo[ii] = no;

                if (sourceTo == (document.getElementById('<%=outletToBox.ClientID%>'))) {
                    hidOutlet.value += no.value + "|";
               }
                if (sourceTo == (document.getElementById('<%=QualMemTypeToBox.ClientID%>'))) {
                    hidMemType.value += no.value + "|";
                }
                if (sourceTo == (document.getElementById('<%=MemStatusToBox.ClientID%>'))) {
                    hidMemStatus.value += no.value + "|";
                }
            }              



        (sourceTo).focus();

            if (sourceTo == (document.getElementById('<%= outletFromBox.ClientID%>'))) {
                (sourceFrom).focus();
            }
            if (sourceTo == (document.getElementById('<%= QualMemTypeFromBox.ClientID %>'))) {
                (sourceFrom).focus();
            }
            if (sourceTo == (document.getElementById('<%= MemStatusFromBox.ClientID %>'))) {
                (sourceFrom).focus();
            }

}

代码背后:

代码语言:javascript
复制
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 PopulateSelectBoxes(hdnMemType, QualMemTypeToBox, QualMemTypeFromBox)

end Sub

 Protected Sub PopulateSelectBoxes(ByVal hdnSelectBox As HiddenField, ByVal selectBox As HtmlSelect, ByVal selectBox_Frm As HtmlSelect)

   Dim hiddenMemType(selectBox.Items.Count - 1) As String
        hiddenMemType = (Split(hdnSelectBox.Value, "|"))

        Dim tempTable As String = ""
        For Each item In hiddenMemType
            If (tempTable.IndexOf(item) = -1) Then
                If item <> "" Then
                    tempTable += item + "|"
                End If
            End If
        Next

        If tempTable <> "" Then
            hiddenMemType = (Split(tempTable, "|"))

            'We remove the items that exist in the ToBox
            For Each item In hiddenMemType
                selectBox_Frm.Items.Remove(item)
            Next

            selectBox.Items.Clear()
            selectBox.DataSource = hiddenMemType
            selectBox.DataBind()
        End If

    End Sub
EN

回答 3

Stack Overflow用户

发布于 2011-11-01 13:22:39

只是在这里大声思考。您在下面的代码中尝试过这种方法吗:

代码语言:javascript
复制
myControl.value = Nothing

也许能行。

编辑:你也可以尝试在里面放一个默认值,如果它是默认值,你就什么也不做了。如果完成了当前值的处理,只需将其更改为默认值即可。

票数 0
EN

Stack Overflow用户

发布于 2011-11-01 13:37:25

尝试禁用隐藏控件上的ViewState --也许这就是保存这些值的原因。

票数 0
EN

Stack Overflow用户

发布于 2011-11-01 13:37:58

在您的JS move函数中,我设想遵循hidMemType.value = "";for循环将再次设置hidMemType.value。你检查过了没有?我会将这个for循环封装在一个if中,以确保它在不应该被击中的情况下不会被击中。

我还会在alert的末尾添加一个function move(),以准确地显示hidMemType.value正在退出的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7962930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档