2013-02-20 38 views
1

所以下面的JavaScript代碼就是我用來從我們的自動營銷軟件Eloqua中提取數據的。這適用於單選下拉菜單。我想要它做的是多選下拉菜單。更改If - Else函數返回多個值?

我知道ProductValue變量有效。所以說,我認爲它是在if(ProductValue == ProductList [i] .value),特別是「.value」,因爲這是調用下拉的值。有沒有辦法讓這個倍數?這讓我堅持了幾天。

function CustomerInformation() 

{ 

var ProductValue = "<span class=eloquaemail>MarketingCustomerInformation1</span>"; //Field Merge...Field merge is working 
var ProductList = document.getElementById('C_Marketing_Customer_Information1').options; //Calling the contact record field 

for(var i=0; i< ProductList.length; i++) 
    {  
     if(ProductValue == ProductList[i].value) 
      { 
       document.getElementById('C_Marketing_Customer_Information1').value = ProductValue; 
       break; 
      } 
     else 
      { 
       document.getElementById('C_Marketing_Customer_Information1').value = "Select"; 
      } 
    } 
} 
+0

你試過擺脫'打破;'? – Blender 2013-02-20 23:02:46

+0

試過這個,但沒有奏效。 – TheCrazyScott 2013-02-21 16:36:59

回答

0

,你可以使用多個for循環:

var ProductValue = "<span class=eloquaemail>MarketingCustomerInformation1</span>"; //Field Merge...Field merge is working 
var ProductList = document.getElementById('C_Marketing_Customer_Information1').options; //Calling the contact record field 

for(var i=0; i< ProductList.length; i++) 
{  
    for(var j=0;j<ProductValue.length;j++) 
    { 
     if(ProductValue[j] == ProductList[i].value) 
     { 
      document.getElementById('C_Marketing_Customer_Information1').option[i].selected="selected"; 
     } 
    } 
} 

還可以添加一個布爾VAR選擇當什麼也沒得到選擇默認。

基本思想

  • 第一個循環會經過多選擇列表中的所有值。
  • 第二個循環會遍歷所有傳遞給它的值(這意味着ProductValue將需要是您想要選擇的所有值的數組)。
  • 如果第一個數組(ProductList [i])中的當前項等於(==)第二個數組中的當前項(ProductValue [j]),那麼您將標記選項爲選中狀態。

爲您JS的一些有用的工具,需要:

  • 谷歌
  • W3Schools.com
  • 螢火蟲(火狐插件,雖然大多數的瀏覽器有什麼綁F12,可以幫助,用它來看到你的變量,並通過功能逐步)
+0

對不起,我的JS是相當有限的。自從我開始這個項目以來,我幾乎一直在教自己。因此,你說這是說運行多個循環?我嘗試使用我的函數CustomerInformation()中提供的代碼,但似乎仍然想出填充正確值的Field Merge,但它拒絕將值傳遞給多選列表。 – TheCrazyScott 2013-02-21 16:40:49

+0

下面是多選列表的代碼: \t \t \t \t \t \t \t \t <選擇多個= 「」 名稱= 「C_Marketing_Customer_Information1」 ID = 「C_Marketing_Customer_Information1」 類= 「elqField」 大小= 「10」 風格=」垂直對齊:頂部; 「> \t \t \t \t \t \t \t \t \t \t <選項值=」」選擇= 「選擇」> - 請選擇 - \t \t \t \t \t \t \t \t \t <選項值= 「蘋果」>蘋果 \t \t \t \t \t \t \t \t \t \t <選項值= 「胡蘿蔔」>胡蘿蔔 \t \t \t \t \t \t \t \t \t \t <選項值=」梨「>梨子 \t \t \t \t \t \t \t \t \t \t <期權價值= 「橙色」>橙色 \t \t \t \t \t \t \t \t \t – TheCrazyScott 2013-02-21 16:42:18

+0

你是如何調用這個函數?有沒有一個按鈕,或者你使用cookie或傳遞參數或什麼? – 2013-02-22 19:00:45