javascript 操作select下拉式清單方塊的一點小經驗

來源:互聯網
上載者:User

按照我一貫的web開發風格,所有不直接操作資料庫的事件,都儘可能由javascript來實現,所以這個需求我打算使用js來完成。
首先來分析一下具體情況:這個頁面是一個更新頁面,品牌有品牌1和品牌2兩個欄位,品牌2可以為空白,品牌1不可為空,所以品牌2的下拉式清單方塊比品牌1多一項;如果選擇了品牌的前8相中的任意一項,“活躍狀態”要隱藏,否則“活躍狀態”預設顯示狀態為“潛在”;當查詢的結果品牌1和品牌2有任意一項在品牌的前8相中,“活躍狀態”也要隱藏,否則“活躍狀態”預設顯示狀態為“潛在”。
頁面部分內容

複製代碼 代碼如下:<div id="div_Brand_Baby" name="div_Brand_Baby" style="display: none" runat="server">
<div style="float: left;">品牌1:</div>
<div style="position: relative; float: left;">
<span style="margin-left: 170px; width: 18px; overflow: hidden;">
<atlas:UpdatePanel ID="UpdatePanel12" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlistFirstConsumeBrand" onchange="ChangeBrand(this)" runat="server"
DataTextField="OptionText" DataValueField="optionValue" DataSourceID="ObjectDataSource11"
Style="width: 188px; margin-left: -170px">
</asp:DropDownList>
</ContentTemplate>
</atlas:UpdatePanel>
</span>
<asp:TextBox ID="txtBrand1" runat="server" onblur="changebrand1(this)" Style="width: 170px;
position: absolute; left: 0px;"></asp:TextBox>
</div>
<div style="float: left;">
品牌2:</div>
<div style="position: relative; float: left;">
<span style="margin-left: 170px; width: 18px; overflow: hidden;">
<atlas:UpdatePanel ID="UpdatePanel13" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlistSecondConsumeBrand" runat="server" onchange="ChangeBrand(this)"
DataTextField="OptionText" DataValueField="optionValue" DataSourceID="ObjectDataSource12"
Style="width: 188px; margin-left: -170px">
</asp:DropDownList>
</ContentTemplate>
</atlas:UpdatePanel>
</span>
<asp:TextBox ID="txtbrand2" runat="server" onblur="changebrand1(this)" Style="width: 170px;
position: absolute; left: 0px;"></asp:TextBox>
</div>
<asp:ObjectDataSource ID="ObjectDataSource11" runat="server" SelectMethod="RetrieveMilkBrand_Baby"
TypeName="CRR.BusinessRules.OptionManager">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="languageID" Type="Int32" />
<asp:Parameter DefaultValue="false" Name="addNull" Type="Boolean" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource12" runat="server" SelectMethod="RetrieveMilkBrand_Baby"
TypeName="CRR.BusinessRules.OptionManager">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="languageID" Type="Int32" />
<asp:Parameter DefaultValue="true" Name="addNull" Type="Boolean" />
<asp:Parameter DefaultValue=" " Name="nullString" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</div>

javascript代碼 複製代碼 代碼如下:function changebrand1(oTextbox)
{
var brandTag=document.getElementById("ddlistSecondConsumeBrand");
var brand1=document.getElementById("txtbrand1");
var brand2=document.getElementById("txtbrand2");
var brandcolls=brandTag.options;
var textvalue=oTextbox.value;
var flag=0;
if(textvalue.length==0)
{
flag=1;
}
else if(textvalue.length>0)
{
for(var i=0;i<brandcolls.length;i++)
{
if(oTextbox==brand1 && brandcolls[i].text==textvalue)
{
document.getElementById("ddlistFirstConsumeBrand").options.selectedIndex=i-1;
flag=1;
ChangeBrand(document.getElementById("ddlistFirstConsumeBrand"));
}
else if(oTextbox==brand2 && brandcolls[i].text==textvalue)
{
brandTag.selectedIndex=i;
flag=1;
ChangeBrand(brandTag);
}
}

if(flag==0)
{
alert("輸入品牌錯誤!");
oTextbox.value="";
oTextbox.focus();
}
}
}

function ChangeBrand(me){
var brand1ID = document.all.ddlistFirstConsumeBrand.value;
var brand2ID = document.all.ddlistSecondConsumeBrand.value;
var brandvalue1=document.getElementById("txtbrand1");
var brandvalue2=document.getElementById("txtbrand2");
if((brand1ID=="10")&&(brand2ID=="-1"))
{
document.all.ddlistMilkPeriod.value=9;
}

for(var i=0;i<document.getElementById("ddlistSecondConsumeBrand").options.length;i++)
{
if(document.getElementById("ddlistFirstConsumeBrand") == me && document.all.ddlistFirstConsumeBrand.selectedIndex==i)
{
brandvalue1.value=document.getElementById("ddlistFirstConsumeBrand").options[i].text;
}
if(document.getElementById("ddlistSecondConsumeBrand") == me && document.all.ddlistSecondConsumeBrand.selectedIndex==i)
{
brandvalue2.value=document.getElementById("ddlistSecondConsumeBrand").options[i].text;
}

if(i<8 && document.getElementById("ddlistFirstConsumeBrand") == me && document.all.ddlistFirstConsumeBrand.selectedIndex==i)
{
document.all.dv1.style.display="block";
document.all.dv2.style.display="none";
document.all.dv3.style.display="none";
document.getElementById("ddlistPotential").options[0].selected="selected";
break;
}
else if(i>0 && i<9 && document.getElementById("ddlistSecondConsumeBrand") == me && document.all.ddlistSecondConsumeBrand.selectedIndex==i)
{
document.all.dv1.style.display="block";
document.all.dv2.style.display="none";
document.all.dv3.style.display="none";
document.getElementById("ddlistPotential").options[0].selected="selected";
break;
}
else if(i>8)
{
document.all.dv1.style.display="none";
document.all.dv2.style.display="block";
document.all.dv3.style.display="block";
document.getElementById("ddlistPotential").options[1].selected="selected";
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.