2017-08-19 38 views
0

我正在嘗試創建一個網站,其中包含一個允許客戶端複製表單的添加按鈕。雖然這樣做,但我正在失去由於重複而存在的隱藏/顯示選項功能。 (ifYesifNo divs,應根據客戶選擇顯示)爲不同ID名稱的div提供內容

我的問題在於我的JavaScript中的變量'Valu',所以我假設。

下面是我的代碼片段:

HTML:

<select id="ValuType" name="ValuType" onchange="priceAndCheck()"> 
    <option disabled="disabled" selected="selected">Type of Property</option> 
    <option id="Apartment" value="Apartment">Apartment</option> 
    <option id="Building" value="Building">Building</option> 
    <option id="Farm" value="Farm">Farm</option> 
    <option id="House" value="House">House</option> 
    <option id="Land" value="Land">Land</option> 
</select> 


<div id="ifYes" class="ifYes"> 
...ifYes content comes here... 
</div> 

<div id="ifNo" class="ifNo"> 
...ifNo content comes here... 
</div> 

的JavaScript:

function yesnoCheck() { 

    var Valu = document.getElementById("ValuType").value; 

    if (Valu == 'Apartment' || Valu == 'Farm' || Valu == 'Land') { 
     document.getElementById('ifYes').style.height = '250px'; 
     document.getElementById('ifYes').style.display = 'block'; 
     document.getElementById('ifYes').style.transition = 'all 1s ease 0.59s'; 
     document.getElementById('ifNo').style.height = '0px'; 
     document.getElementById('ifNo').style.overflow = 'hidden'; 
     document.getElementById('ifNo').style.transition = '0.55s'; 
    } 
    else if (Valu == 'Building' || Valu == 'House') { 
     document.getElementById('ifNo').style.height = '250px'; 
     document.getElementById('ifNo').style.display = 'block'; 
     document.getElementById('ifNo').style.transition = 'all 1s ease 0.59s'; 
     document.getElementById('ifYes').style.height = '0px'; 
     document.getElementById('ifYes').style.overflow = 'hidden'; 
     document.getElementById('ifYes').style.transition = '0.55s'; 
    } 
    else { 
     document.getElementById('ifYes').style.overflow = 'hidden'; 
     document.getElementById('ifNo').style.overflow = 'hidden'; 
    } 
} 



function providePrice() { 

var a = document.getElementById("Region").value; 
var b = document.getElementById("ValuReq").value; 
var c = document.getElementById("ValuType").value; 
var d = document.getElementById("Express").value; 
var e = 25; 




if (a=="Region1" && b=="Bank" && c=="Apartment" && d=="Yes") 
{ 
    var x = 200 + e; 
    document.getElementById("price").innerHTML = "Price: OMR " + x; 

} 

else if (a=="Region1" && b=="Bank" && c=="Apartment" && d=="No") 
{ 
    var x = 200; 
    document.getElementById("price").innerHTML = "Price: OMR " + x; 
} 

else if (a=="Region1" && b=="Bank" && c=="Building" && d=="Yes") 
{ 
    var x = 350 + e; 
    document.getElementById("price").innerHTML = "Price: OMR " + x; 
} 
... 
... 
... 
else 
{ 
    document.getElementById("price").innerHTML = "Price:" 
} 


    function getValue() { 
if (document.getElementById("Express").checked) { 
    document.getElementById("Express").value = "Yes"; 
} else { 
    document.getElementById("Express").value = "No"; 
} 
} 


function priceAndCheck(){ 
    yesnoCheck(); 
    providePrice(); 
} 

代碼說明:
每當客戶填寫表格,並選擇某些選項,它應該允許div ifYesifNo顯示和價格將被填充。 (價格連接到多個字段)我正在使用div這樣的名稱來完成此操作。但是,在克隆表單時,我無法爲所有新克隆表單生成div ifYesifNo

在哪裏我相信問題在於在以下幾點:

  • div namings是不變(完全一樣在所有的克隆形式),這是問題
  • 一個調用priceAndCheck()函數調用兩個函數yesnoCheck()providePrice(),這使得我需要改變所有功能,如果我開始調用一個元素(例如function test(selectElement) - 嘗試[Sam Apostel的解決方案] [1],並且無法根據需要查看我的所有可用編碼的選項
  • 被克隆的div有不同的id名稱,但都在其他divs具有相同的命名

將是對什麼是處理這個問題的最佳途徑一定的幫助表示感謝。

+0

何時何地你叫'功能yesnoCheck()'? –

+0

@irshadjm對不起,忘記了當我改變選項時我調用了兩個函數。 onchange =「priceAndCheck()」在其中具有函數yesnoCheck()。 –

+1

你面臨的問題,因爲一旦你的表單被複制,你將失去使用ID獲取對象的功能,因爲現在你有多個具有相同ID的元素,這是不允許的,在這種情況下,你總是會得到最後一個元素的引用,如果你的嘗試通過ID來訪問它,我建議你更新你的邏輯來逐個獲取元素。 –

回答

0

後多testings和代碼編輯,我想出了下面的解決我的查詢哪裏我利用每個克隆表單的類名稱,根據需要,選項變得可見。

function yesnoCheck() { 
 
\t \t 
 
\t \t var Valu =''; 
 
\t \t var count = 0; 
 
\t 
 
\t \t $('.ValuType').each(function() { 
 
\t \t \t 
 
\t \t \t Valu = $(this).attr('id'); 
 
\t \t \t Valu = document.getElementById(Valu).value; 
 
\t \t \t 
 
\t \t \t 
 

 
\t \t  if (Valu == 'Apartment' || Valu == 'Farm' || Valu == 'Land') { 
 
\t \t \t \t document.getElementsByClassName('Yes')[count].style.height = '30px'; 
 
\t \t   document.getElementsByClassName('Yes')[count].style.display = 'block'; 
 
\t \t \t \t document.getElementsByClassName('Yes')[count].style.transition = 'all 1s ease 0.59s'; 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.height = '0px'; 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.overflow = 'hidden'; 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.transition = '0.55s'; 
 
\t \t  } 
 
\t \t \t else if (Valu == 'Building' || Valu == 'House') { 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.height = '30px'; 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.display = 'block'; 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.transition = 'all 1s ease 0.59s'; 
 
\t \t \t \t document.getElementsByClassName('Yes')[count].style.height = '0px'; 
 
\t \t \t \t document.getElementsByClassName('Yes')[count].style.overflow = 'hidden'; 
 
\t \t \t \t document.getElementsByClassName('Yes')[count].style.transition = '0.55s'; 
 
\t \t \t } 
 
\t \t  else { 
 
\t \t \t \t document.getElementsByClassName('Yes')[count].style.overflow = 'hidden'; 
 
\t \t \t \t document.getElementsByClassName('No')[count].style.overflow = 'hidden'; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t count++; 
 
\t \t }); 
 

 
\t } 
 
    
 
    
 
    
 
    //define template 
 
var template = $('#content:first').clone(); 
 

 

 
//define counter 
 
var count = 0; 
 

 
//add new section 
 
$('body').on('click', '#repeat', function() { 
 

 
\t var clone = template.clone(); 
 
    //increment 
 
    count++; 
 
\t 
 
\t //remove cross button div 
 
\t clone.find('cross').removeAttr("#cross"); 
 
\t 
 
\t //update id of ifYes & ifNo 
 
\t clone.find('.Yes').prop('id', 'ifYes' + count); 
 
\t clone.find('.No').prop('id', 'ifNo' + count); 
 
\t 
 
    //loop through each input 
 
    var inputSection = clone.find(':input').each(function(){ 
 

 
     //set id to store the updated section number 
 
     var newId = this.id + count; 
 

 
     //update id 
 
     this.id = newId; 
 

 
    }).end() 
 
\t 
 
    //inject new section with animation 
 
    .appendTo('#content').hide() 
 
\t .appendTo('#content').slideDown(500) 
 
    return false; 
 
}); 
 

 
//remove section 
 
$('#content').on('click', '.cross', function() { 
 
    //slide up section 
 
    $(this).parent().slideUp(500, function(){ 
 
     //remove parent element (main section) 
 
     $(this).parent().child().empty(); 
 
     return false; 
 
    }); 
 
    return false; 
 
});
.Yes{ 
 
    overflow: hidden; 
 
    height: 0; 
 
    -webkit-transition: all 1.5s ease; 
 
    -moz-transition: all 1.5s ease; 
 
    -ms-transition: all 1.5s ease; 
 
    -o-transition: all 1.5s ease; 
 
    transition: all 1.5s ease; 
 
} 
 

 

 
.No{ 
 
    overflow: hidden; 
 
    height: 0; 
 
    -webkit-transition: all 1.5s ease; 
 
    -moz-transition: all 1.5s ease; 
 
    -ms-transition: all 1.5s ease; 
 
    -o-transition: all 1.5s ease; 
 
    transition: all 1.5s ease; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="content"> 
 
    <button type="button" id="cross" class="buttonImgTop cross">remove</button> 
 
    <div id="ValuWrapper"> 
 
    
 
    <select id="ValuType" name="ValuType" class="ValuType" onchange="yesnoCheck()"> 
 
\t <option disabled="disabled" selected="selected">Type of Property</option> 
 
\t <option id="Apartment" value="Apartment">Apartment</option> 
 
\t <option id="Building" value="Building">Building</option> 
 
\t <option id="Farm" value="Farm">Farm</option> 
 
\t <option id="House" value="House">House</option> 
 
\t <option id="Land" value="Land">Land</option> 
 
    </select> 
 
        
 
     <div id="ifYes" class="Yes"> 'Yes' class text comes here </div> 
 
     <div id="ifNo" class="No"> 'No' class text comes here </div> 
 
    </div> 
 
</div> 
 

 
<button type="button" class="buttonImg" id="repeat">repeat</button>

0

我認爲這將解決你的問題(在onchange=""標籤使用this

$ = function(id) { 
 
    return document.getElementById(id).style; 
 
} 
 
function priceAndCheck(selectElement){ 
 

 
yesnoCheck(selectElement); 
 
} 
 
function yesnoCheck(selectElement) { 
 
    var show; 
 
    var hide; 
 
    var Valu = selectElement.value; 
 
    if (Valu == 'Apartment' || Valu == 'Farm' || Valu == 'Land') { 
 
    show = $('ifYes'); 
 
    hide = $('ifNo'); 
 
    } else if (Valu == 'Building' || Valu == 'House') { 
 
    show = $('ifNo'); 
 
    hide = $('ifYes'); 
 
    } 
 
    show.height = '250px'; 
 
    show.display = 'block'; 
 
    show.transition = 'all 1s ease 0.59s'; 
 
    hide.height = '0px'; 
 
    hide.overflow = 'hidden'; 
 
    hide.transition = '0.55s'; 
 
}
<select id="form1" name="ValuType" onchange="priceAndCheck(this)"> 
 
    <option disabled="disabled" selected="selected">Type of Property</option> 
 
    <option id="Apartment" value="Apartment">Apartment</option> 
 
    <option id="Building" value="Building">Building</option> 
 
    <option id="Farm" value="Farm">Farm</option> 
 
    <option id="House" value="House">House</option> 
 
    <option id="Land" value="Land">Land</option> 
 
</select> 
 

 

 
<div id="ifYes" class="ifYes"> 
 
    ...if Yes content comes here... 
 
</div> 
 

 
<div id="ifNo" class="ifNo"> 
 
    ...if No content comes here... 
 
</div>

+0

無法讓它與我的代碼一起工作,因爲我有兩個功能在選項更改時同時運行。我可以不在我打電話的函數中添加'this.id'嗎?如: priceAndCheck(){ yesnoCheck(this.id); function2(); } –

+0

我改變了我的答案,以適應嵌套函數,並將'this.id'改爲'this',這樣就不必查看DOM來找到ellement –

+0

仍然不起作用。看起來,如果我調用兩個函數,我不能使用這個方法,除非我改變我的第二個函數,或者我假設。 –