2017-04-21 34 views
0

最近我對我的工作有任務,這對我來說太「大」了。混合了兩個列表的輸出

我期待創建混合兩個元素的發生器。

  1. 的位置列表
  2. 代碼或文本
  3. 輸出將進入位置碼/文本爲每個新線

這是HTML當前工作

Screenshot of generator

因爲我在PHP很漂亮,可以有人解釋什麼函數我應該使用這臺發電機嗎?

這是我的代碼:

<div class="container"> 
<div class="col-lg-12"> 
    <h1 style="text-align:center; margin-bottom:50px;">Generate Locations  </h1> 
</div> 

<div class="col-lg-6"> 

    <h3>Input locations in new line each</h3> 

    <textarea rows="20" style="width:100%"> 
    </textarea> 
</div> 

<div class="col-lg-6"> 
    <h3>Input code. Use "??Location" to replace.</h3> 

    <textarea rows="20" style="width:100%"> 
    </textarea> 
</div> 

<center><button type="button" class="btn btn-primary" style="margin-top:20px;" >Generate</button></center> 

<div class="col-lg-12"> 
    <h3>Output</h3> 

    <textarea rows="20" style="width:100%"> 
    </textarea> 
</div> 

+0

是截圖你提供展示當前的工作,或者是你的要求嗎?加上PHP是必要的,因爲如果你只想輸出結果,也可以通過js來完成 –

+0

截圖顯示了我的html工作至今,現在我需要php的功能。還有,Javascript對我來說也不錯。我只需要幫助我可以使用哪些功能,如何獲取第一個列表,檢測位置和導出 –

+0

是否有任何特殊原因希望在PHP中執行此操作?它可以在JavaScript中輕鬆完成,而不需要提交和加載頁面。 – Thakkie

回答

0

首先,給你的文本域ID,這樣你可以很容易地引用它們:

<textarea rows="20" style="width:100%" id="locations"> 
</textarea> 

<textarea rows="20" style="width:100%" id="inputcode"> 
</textarea> 

<textarea rows="20" style="width:100%" id="output"> 
</textarea> 

其餘的Javascript:

<script> 
    function generateLocations() { 
     var locations = document.getElementById("locations").value.split(/[\r\n]+/g); 
     var inputcode = document.getElementById("inputcode").value; 

     var output_text = ""; 

     locations.forEach(function (item, index, array) { 
      output_text += inputcode.replace(/\?\?Location/g, item) + "\n"; 
     }); 

     document.getElementById("output").value = output_text; 
    } 
</script> 

你可以c所有這些功能與您的按鈕:

<button onclick="generateLocations()" type="button" class="btn btn-primary" style="margin-top:20px;">Generate</button> 
+0

謝謝!我認爲它的功能和onclick,它'完美的作品。只有一件事,我怎麼可以添加這樣的輸出到每個新行? –

+0

當然,勾選標記是強制​​性的,但是謝謝你保存我的**在這是最重要的事情:) –

0

增加號碼locationscodeoutput分別定位的,代碼的輸出的文本區域。

在腳本標記添加此

function generate(){ 
    var lta = document.getElementById("locations"); 
    var cta = document.getElementById("code"); 

    var locations = lta.split("\n"); 
    var codes = cta.split("\n"); 
    var final = ""; 
    codes.forEach(function(code){ 
     locations.forEach(function(loc){ 
      var codeCopy = code; 
      final += codeCopy.replace("??Location", loc) + "\n"; 
     }) 
    }) 
    var out = document.getElementById("output"); 
    out.innerText = final; 
} 

調用它產生按鈕onclick

+0

謝謝穆罕默德,我沒有檢查你的代碼,因爲法老已經提供了我的工作。謝謝你的時間! –