单选按钮首先被禁用,然后再次启用提交

Radio button disabled first, after submit again enabled

本文关键字:然后 启用 提交 单选按钮      更新时间:2023-09-26

我有一个jsp,其中一些单选按钮和文本框取决于下拉选择
这是我的jsp和javascript代码

jsp

         <s:form action="crInquiry" name="form">    
            <s:select name="filterValue" list="headerList"
    onchange="OnChange(this.form.filterValue);" />
            <s:radio name="filter" list="#{'STATUS_FILTER_START':'START','STATUS_FILTER_END':'STOP'}" 
label="Stage"></s:radio>
            <s:textfield disabled="true" value="0" name="count" theme="css_xhtml"></s:textfield>
            <s:radio name="ascOrder"  list="#{'ASC':'ASC','DESC':'DESC'}"></s:radio>
            <s:submit value="Filter" onclick="gotopage('FilteredInquiryLog')"></s:submit>
            <s:submitvalue="Details" onclick="gotopage('crInquiry')"></s:submit>
            </s:form>

javascript

<script type="text/javascript">
    function OnChange(dropdown) {
        var myindex = dropdown.selectedIndex;
        document.form.filter[0].disabled = false;
        document.form.filter[1].disabled = false;
    if (myindex == 8) {
            alert("8");
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = false;
            document.form.submit.disabled = false;
        }
        else if (myindex == 9) {
            alert("9");
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
        }
        else{
            document.form.filter[0].disabled = false;
            document.form.filter[1].disabled = false;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
            }
        }

    function gotopage(actionname)
    {   
            document.form.action=actionname+".action";
            document.form.submit();
    }
</script>

问题是,当我在下拉列表中选择一个项目时,比如元素编号8或9,因此通过javascript,应该禁用这两个项目的单选按钮,并禁用9的文本字段和单选按钮。当我选择一个项目时,它完全禁用了相关的单选按钮或文本字段,但当我提交时,它会显示启用的单选按钮,因为我使用的是同一个jsp。我的javascript有什么问题?

您还应该在页面加载事件中检查下拉值。

像这样的

<script type="text/javascript">
/**
* This function will be called twice - once on onChage event, and once on onLoad event
*/
    function OnChange() {
    dropdown = document.getElementById('myDropDown');
        var myindex = dropdown.selectedIndex;
        document.form.filter[0].disabled = false;
        document.form.filter[1].disabled = false;
    if (myindex == 8) {
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = false;
            document.form.submit.disabled = false;
        }
        else if (myindex == 9) {
            document.form.filter[0].disabled = true;
            document.form.filter[1].disabled = true;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
        }
        else{
            document.form.filter[0].disabled = false;
            document.form.filter[1].disabled = false;
            document.form.count.disabled = true;
            document.form.submit.disabled = false;
            }
        }

    function gotopage(actionname)
    {   
            document.form.action=actionname+".action";
            document.form.submit();
    }
</script>
<body onload="OnChange()">
<s:form action="crInquiry" name="form">    
            <s:select name="filterValue" list="headerList" id="myDropDown"
    onchange="OnChange();" />
            <s:radio name="filter" list="#{'STATUS_FILTER_START':'START','STATUS_FILTER_END':'STOP'}" 
label="Stage"></s:radio>
            <s:textfield disabled="true" value="0" name="count" theme="css_xhtml"></s:textfield>
            <s:radio name="ascOrder"  list="#{'ASC':'ASC','DESC':'DESC'}"></s:radio>
            <s:submit value="Filter" onclick="gotopage('FilteredInquiryLog')"></s:submit>
            <s:submitvalue="Details" onclick="gotopage('crInquiry')"></s:submit>
            </s:form>

如果我正确理解您的问题,我认为您的Javascript没有问题
因为在提交之前和提交之后页面数据之间没有通信,这将产生问题。

你可以通过以下方法之一来克服这个问题。1.在body的"OnLoad"事件上调用您的javascript,它将对您起同样的作用
2.将页面的当前状态存储在一些控件(比如文本字段)中,并与页面的其他数据一起提交。在加载时(可能是onLoad或JSP页面的末尾),您可以使用该字段中的值来恢复状态。

我希望这对你有用。