2016-10-28 77 views
0

我想以很多字符串的形式接受用戶輸入。我想將它們存儲在一個數組中,並且輸入應該通過換行符分開。用Javascript輸入用戶輸入(用換行符分隔)用

它應該是非常喜歡這樣的:https://www.random.org/lists/

我無法掌握在何處存在 - 有人可以幫忙嗎?我正在使用JavaScript,但任何使用JS或jQuery的解決方案都會很棒!

我發佈了我的JS。我想從用戶輸入的var人,而不是自己填充陣列。

感謝,

$(document).ready(function() { 
    $(".btn").on('click', function() { 
    var people = ["Markus Eriksson", "Leticia Hoshino", "Yemi Afolabi", "Eskil Fogelström", "Josefina Liedberg", "David Bjørn Bograd", "Tilda Dahlgren", "Damien Vignol", "Sofie Cousu", "Carolina Lindelöw", "Bilal Khan", "Louise Brandrup-Wognsen", "Emilia Lehto", "Albin Hagström", 
     "Victor Borg", "Anna Stella Lo-Ré", "Loucmane", "Angelica Ruth", "Victoria VL", "Johan Hellström", "Micke Skoglund", "Anna Unger", "Isaac Sennerholt", "Cyndie Léa Vintilescu", "Mahle Rakela Robin", "Louise Ek", "Ibrahim Bajwa", "Abodi Ismail", 
     "Alex Ashman", "Elin Grass Casalini", "Amanda Schultz", "Abenezer Abebe", "Julia Hoff", "Enny Hellsén", "Michel George", "Abdullahi Hussein", "Teodor Meurling", "Andrea Sami Mogren", "Thea Arpine Gasparyan", "Jakob Eberson" 
    ]; 
    var groupSize = $("input[name=checkListItem]").val(); 
    var groups = []; 

    $(".group").remove(); 

    // Randomizing function 
    Array.prototype.shuffle = function() { 
     var input = this; 

     for (var i = input.length - 1; i >= 0; i--) { 

     var randomIndex = Math.floor(Math.random() * (i + 1)); 
     var itemAtIndex = input[randomIndex]; 

     input[randomIndex] = input[i]; 
     input[i] = itemAtIndex; 
     } 
     return input; 
    }; 

    people.shuffle(); 

    // Split people into chunks and push new arrays into var groups 
    while (people.length > 0) { 

     chunks = people.splice(0, groupSize); 
     var chunksSpace = chunks.join(', '); 

     groups.push(chunksSpace); 
    } 

    // Append the groups into the DOM 
    $(document).ready(function() { 
     for (var i = 0; i < groups.length; i++) { 
     $('.all-groups').append("<div class='group'><p><span class='groupheader'>Group " + (i + 1) + "</span></br> " + groups[i] + "</p></div>"); 
     } 
    }); 
    }); 
}); 
+0

向我們展示您迄今爲止編寫的代碼。 –

+0

@KhorshedAlam我現在添加了我用來隨機化數組的JS代碼。我想從用戶輸入的是var人。 – davidbograd

+0

JS document.getElementById(「element_id」).value.split(「\ n」);或者JQuery $(「#element_id」)。val().split(「\ n」); –

回答

0

純JavaScript

document.getElementById("element_id").value.split("\n"); 

或JQuery的$("#element_id").val().split("\n");

對於你的例子給你輸入id='people'建議立即進行刪除d工作,也避免了.replace(/\n+/g,"\n")的額外換行符。

$(document).ready(function() {  
    // Randomizing function 
    Array.prototype.shuffle = function() { 
     var input = this; 

     for (var i = input.length - 1; i >= 0; i--) { 

     var randomIndex = Math.floor(Math.random() * (i + 1)); 
     var itemAtIndex = input[randomIndex]; 

     input[randomIndex] = input[i]; 
     input[i] = itemAtIndex; 
     } 
     return input; 
    }; 

    $(".btn").on('click', function() { 
    var people = $("#people").val().replace(/\n+/g,"\n").split("\n"); 
    var groupSize = $("input[name=checkListItem]").val(); 
    var groups = []; 

    $(".group").remove(); 

    people.shuffle(); 

    // Split people into chunks and push new arrays into var groups 
    while (people.length > 0) { 

     chunks = people.splice(0, groupSize); 
     var chunksSpace = chunks.join(', '); 

     groups.push(chunksSpace); 
    } 

    // Append the groups into the DOM 
    $(document).ready(function() { 
     for (var i = 0; i < groups.length; i++) { 
     $('.all-groups').append("<div class='group'><p><span class='groupheader'>Group " + (i + 1) + "</span></br> " + groups[i] + "</p></div>"); 
     } 
    }); 
    }); 
}); 
+0

非常感謝你Mamdouh,也是爲了解釋和使用上下文中的代碼。讓我明白髮生了什麼,現在它正在爲我工​​作! – davidbograd

+0

不客氣,我很高興隨時爲您提供幫助:) –

0
var text = $('#total-number').text(); 
var eachLine = text.split('\n'); 
    alert('Lines found: ' + eachLine.length); 
    for(var i = 0, l = eachLine.length; i < l; i++) { 
     alert('Line ' + (i+1) + ': ' + eachLine[i]); 
    } 
+0

儘管此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – andreas

0

使用split到多線串分成幾部分:

var textarea = document.querySelector("textarea"); 
 

 
textarea.addEventListener("change", function(e) { 
 
    console.log(textarea.value.split((/[\n\r]/g))); 
 
});
<textarea></textarea>

正則表達式鏈接: