2017-01-03 85 views
1

我需要保留前一個if語句的值,然後在下一個if語句中輸出來自前一個if語句的值(其值爲相同值)(authorId = 2)。從以前的if語句中取值

我的意思是我需要保留if語句的值(authorId === 1),然後在下一個if語句(authorId = 2)中打印出另一個值。 因此,我需要保留authorSurname.value(id = 1)並將其作爲secondAuthor.value打印在if語句(authorId = 2)中,因爲在if語句(authorId = 2)中,字符串authorSurname需要另一個值。我的代碼是:

你能告訴我如何解決它?

<!DOCTYPE HTML> 
<html lang='pl'> 
<head> 
    <meta charset='utf-8'> 
    <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'> 
    <link rel='stylesheet' href='css/style.css'> 

<!-- dynamic form - script --> 

<script type="text/javascript"> 



window.onload = Load; 
var numberOfAuthors = 0; 


function Load() 
{ 
    document.getElementById('add_input').onclick = AddElement; 
} 

function AddElement() 
{ 
    var element1 = document.createElement('input'); 
    var element2 = document.createElement('input'); 
    var element3 = document.createElement('input'); 
    var label1 = document.createElement('label'); 
    var label2 = document.createElement('label'); 
    var label3 = document.createElement('label'); 
    var button = document.createElement('input'); 

    var number = numberOfAuthors; 

    label1.innerHTML = "</br>Next Author's Name "+"</br>"; 
    label1.setAttribute('id', 'authorNameLabel' + number); 

    element1.setAttribute('type', 'text'); 
    element1.setAttribute('id', 'authorName' + number); 
    element1.setAttribute('placeholder', "author's name"); 
    label1.appendChild(element1); 

    label2.innerHTML = "</br>Next Author's Initials " + "</br>"; 
    label2.setAttribute('id', 'authorInitialsLabel' + number); 

    element2.setAttribute('type', 'text'); 
    element2.setAttribute('id', 'authorInitials' + number); 
    element2.setAttribute('placeholder', "Author's Initials..."); 
    label2.appendChild(element2); 

    label3.innerHTML = "</br>Next Author's surname" + '</br>'; 
    label3.setAttribute('id', 'authorSurnameLabel' + number); 

    element3.setAttribute('type', 'text'); 
    element3.setAttribute('id', 'authorSurname' + number); 
    element3.setAttribute('placeholder', "Author's surname..."); 
    label3.appendChild(element3); 

    button.setAttribute('onclick', 'removeAuthor(' + number + ')'); 
    button.setAttribute('type', 'button'); 
    button.setAttribute('id', 'removeAuthorButton' + number); 
    button.setAttribute('value', 'Button'); 

    document.forms['add_file'].appendChild(label1); 
    document.forms['add_file'].appendChild(label2); 
    document.forms['add_file'].appendChild(label3); 
    document.forms['add_file'].appendChild(button); 

    numberOfAuthors++; 
} 


function removeELement(id) { 
    var elem = document.getElementById(id); 
    return elem.parentNode.removeChild(elem); 
} 

function removeAuthor(authorId){ 
    removeELement("authorName"+authorId); 
    removeELement("authorInitials"+authorId); 
    removeELement("authorSurname"+authorId); 
    removeELement("removeAuthorButton"+authorId); 
    removeELement("authorNameLabel"+authorId); 
    removeELement("authorInitialsLabel"+authorId); 
    removeELement("authorSurnameLabel"+authorId); 
    numberOfAuthors--; 
    getText(); 
} 

function getText(){ 

    console.log(numberOfAuthors); 

    var div = document.getElementById("readyorder"); 

    var firstAuthorName = document.getElementById("firstAuthorName"); 
    var firstAuthorInitials = document.getElementById("firstAuthorInitials"); 
    var firstAuthorSurname = document.getElementById("firstAuthorSurname"); 
    // var secondAuthorSurname = 


    for(var authorId = 0 ; authorId < numberOfAuthors ; authorId++){ 

     var authorName = document.getElementById("authorName"+authorId); 
     var authorInitials = document.getElementById("authorInitials" + authorId); 
     var authorSurname = document.getElementById("authorSurname" + authorId); 


    } 

    var publisher = document.getElementById("publisher"); 

    var page = document.getElementById("page"); 

    var pageOther = document.getElementById("pageOther"); 
    var pageOtherValue = pageOther.value; 

    if(pageOther.value!=""){ 
     pageOtherValue = "-" + pageOther.value; 
    }else{ 
     pageOtherValue = ""; 
    } 

    var year = document.getElementById("year"); 
    var secondAuthorSurname; 
    if(authorId === 0) { 

    div.innerHTML = firstAuthorSurname.value + " (" + year.value + ", s."+page.value + pageOtherValue + ") states that, że \".......\" " + ")"} 

    else if (authorId === 1) { 
      div.innerHTML = firstAuthorSurname.value + " i " + authorSurname.value + " (" + year.value + ") state that \".......\"; 
      var secondAuthorSurname = authorSurname.value; 
    } 

     else if (authorId === 2) { 
// I need to keep the value from authorSurname.value from the if statement above and use it as the secondAuthorSurname below 
//authorSurname takes different value because of the loop 
      var secondAuthorSurname = authorSurname; 
      div.innerHTML = firstAuthorSurname.value + ", " + secondAuthorSurname.value + " and " + authorSurname.value + " (" + year.value + ") state that \".......\"; 

    } 
} 

function handlePages(){ 

    var cboxPageRange = document.getElementById("cboxPageRange"); 

    if (cboxPageRange.checked){ 
     var pageOther = document.getElementById("pageOther"); 
     pageOther.style.display="block"; 
    }else{ 
     var pageOther = document.getElementById("pageOther"); 
     pageOther.style.display="none"; 
     pageOther.value=""; 
     getText(); 
    } 

} 



    </script> 



    </head> 
<body> 

<div class='container'> 

    <!-- header --> 
    <header> 
     <img src="images/header.jpg" alt=""/> 
    </header> 

    <!-- static form 1 --> 

    First Author's Name <br /> 
    <input type="text" id="firstAuthorName" /> <br /> 
    First Author's Initials <br /> 
    <input type="text" id="firstAuthorInitials" /> <br /> 
    First Author's Surname <br /> 
    <input type="text" id="firstAuthorSurname" /> <br /> 

    <!-- dynamic form --> 

<input type="submit" value="Add author" id="add_input" /> 

<form name="add_file" enctype="multipart/form-data" method="post"> 
</form> 



<div id="readyorder"></div><br/> 

    <!-- static form 2 --> 

     Publisher<br /> 
     <input type="text" id="publisher"><br /> 
     Page<br /> 
     <input type="text" id="page"> 
     <input type="checkbox" id="cboxPageRange" value="pageRangeCheckbox" onclick="handlePages()"> 
     <input type="text" id="pageOther" class="pageOther"> 
     <br> 
     Year<br> 
     <input type="text" id="year"><br/> 
     <input type="submit" value="check" onclick="getText()" /> <br/><br/> 

     <!-- readyorder --> 

    <div id="readyorder"></div><br/> 

    <!-- sidebar --> 
    <aside> 
     <nav> 
      <ul> 
       <li><a href="index.html">Main</a></li> 
       <li><a href="#">Change style</a></li> 

      </ul> 
     </nav> 
    </aside> 


    <!-- main --> 
    <section id="main"> 
     <h1>Take your order!</h1><br/> 
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras condimentum tempus mi, maximus volutpat urna sollicitudin vitae. Vivamus rutrum mi sit amet commodo rutrum. Suspendisse potenti. Sed a ullamcorper eros. Maecenas dapibus erat mi, a egestas ipsum cursus volutpat. Aliquam posuere mi at consectetur convallis. Cras vitae ligula eget leo ultrices hendrerit nec sed ex. Morbi at ipsum rhoncus, dictum elit in, consectetur lorem. Aliquam suscipit diam sit amet mauris luctus, a egestas magna pharetra. Donec laoreet viverra risus nec fermentum. Maecenas gravida lectus vel ante commodo bibendum. Donec ac pellentesque mi. </p> 
    </section> 

    <!-- footer --> 
    <footer> 
     <p>Lorem Ipsum © 2017</p> 
    </footer> 

</div> 

</body> 
</html> 
+0

如果塊返回在div.innerHTML之前,最後一個if塊中的'div.innerHTML = firstAuthorSurname.value + ...'永遠不會執行 – Andreas

+1

。 –

+0

所以你想我把div.inner.HTML後面的返回塊? – lookasz

回答

0

一個很明顯的一點毛病是這樣:

else if (authorId === 2) { 
    return secondAuthorSurname; 
    div.innerHTML = firstAuthorSurname.value + "...blabla"; 
} 

什麼是下return值將不會被執行。然而,這幾乎沒有敏感,並與你提供的片段,我們只是猜測。

var secondAuthorSurname; 

if (authorId === 0) { 
    div.innerHTML = firstAuthorSurname.value + "...blabla"; 
    secondAuthorSurname = "your desired return value"; 
} else if (authorId === 1) { 
    div.innerHTML = firstAuthorSurname.value + "...blabla"; 
    secondAuthorSurname = "your desired return value"; 
}  else if (authorId === 2) { 
     div.innerHTML = firstAuthorSurname.value + "...blabla"; 
     secondAuthorSurname = "your desired return value"; 
} 
return secondAuthorSurname; 

另外,VAR不應在if block聲明,這不是構建代碼的一個很好的方式,它不再是可讀的,很多人可能會想到它是未定義/的ReferenceError(即使壽它不是)。首先聲明它。

在環路

(AUTHORID = 3)

什麼循環?

最後編輯我做:

  • 1。你應該知道你缺少一些基本概念,這在你的代碼中很清楚。

第一個if(){}else{}不是一個循環它是一個條件。一個循環可以運行多次,比如while(){}

Secundo,你誤解了var的工作原理。當你聲明一個var時,它在一個範圍內聲明。

,如果你這樣做:

var a = 2; 
var b = 1; 
function foo(){ 
    var a = 3; 
    console.log(a); // 3 
    console.log(b); // 1 
} 

首先它試圖在foo的範圍內得到,它發現它,所以它打印3.然後它試圖找到b在foo的範圍內,不找到它並詢問容器的作用範圍是什麼回答是的,我有b,這裏是1.

雖然你可能知道。然而你不知道的是:

function foo(){ 
    if(true){ 
     u = 1; 
     console.log(u); 
    }else{ 
     var u = 5; 
    } 
} 
foo(); 

你覺得這裏發生了什麼?猜測一下,試着想想幕後發生的事情。

這裏是幕後發生的事情:在執行您的代碼之前

function foo(){ 
    var u; 
    if(true){ 
     u = 1; 
     console.log(u); 
    }else{ 
     u = 5; 
    } 
} 
foo(); 

的VAR在上面放。因此,將變量聲明放在循環之前而不是內部是很好的做法,因爲有些人可能認爲它們沒有聲明(就像其他編程語言一樣)。


話雖這麼說這裏是你的問題簡單化了:

var a = 0; 
var b = 3; 
if (authorId === 1) { 
    a = 1; 
}else if (authorId === 2) { 
    // You are claiming that b changes because of the condition above 
    // this is strictly impossible 
} 

如果進入else塊,那麼你就沒有了,如果反之亦然進入。這就是它的用途。因此,如果塊對你的else塊有影響,就沒有辦法。


隨着我說的一切,你應該看到你的代碼中有許多illogisme。

for(var authorId = 0 ; authorId < numberOfAuthors ; authorId++){ 
    var authorName = document.getElementById("authorName"+authorId); 
} 

這裏的authorname將會從循環的最後一次迭代獲得值。因此,當authorId爲0時,它將等於ID爲「authorInitials0」的元素,然後下一次迭代將被「authorInitials1」替換,依此類推。

換句話說,你可以用一條線代替它,它會產生相同的效果。

var authorName = document.getElementById("authorName"+ (numberOfAuthors -1)); 

這使得小SENS還有:

var pageOther = document.getElementById("pageOther"); 
var pageOtherValue = pageOther.value; 

if(pageOther.value!=""){ 
    pageOtherValue = "-" + pageOther.value; 
}else{ 
    pageOtherValue = ""; 
} 

深吸一口氣,重新學習的基礎知識,不然你會不斷地卡住。也可以在js文件的頂部使用「use strict」。

+0

我應該寫在「if(authorId === 3) – lookasz

+0

@lookasz好吧,如果它幫助upvote和接受答案 – Ced

+0

好吧,你可以更改代碼嗎?它仍然不起作用 – lookasz