2017-09-07 3 views
-6

私はHTMLでリストを作成したいと思っています。そして、ユーザはリストを選択して、選択したものを何かします。jqueryまたはjavascriptでhtmlリストを作成するにはどうすればよいですか?

たとえば、果物のリスト。アップルバナナ、ブドウ。

ユーザーがブドウを選択し、ブドウが選択されている場合、ブドウの数が尋ねられます。

私は私はJavascriptやjQueryの

でこれを行うことができますどのように知っていただきたいと思い

+2

あなたは何を試してみましたか?これまでに書いたコードを投稿できますか? – Stuart

+1

ヘルプページ、特に「[ここではどのトピックについて聞くことができますか」](http://stackoverflow.com/help/on-topic)と[私は尋ねるのを避けますか? "](http://stackoverflow.com/help/dont-ask)。さらに重要なことは、[Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922)をお読みください。また、[最小、完全、および検証可能な例](http://stackoverflow.com/help/mcve)についても知りたいことがあります。 –

答えて

1

このコードブロックを使用してください。そのような単純な、

//generates the list 
 
var items = 'Apple, bannana, grapes'; 
 
var nHtml = ''; 
 
items = items.split(','); 
 
items.forEach(function(v){ 
 
    //trim() will remove the blank spaces 
 
    nHtml+='<li>'+ v.trim() +'</li>'; 
 
}); 
 
$('#itemList').append('<ul>'+ nHtml +'</ul>'); 
 

 
//detect click on the list item 
 
$('li').click(function(){ 
 
    var name = $(this).text(); 
 
    var question = 'How many '+name+' ?'; 
 
    $('#question').html('<span>'+ question +'</span><input type="text" id="answer" />') 
 
}); 
 

 
//get the number feed into the text box 
 
function getCount(){ 
 
    var answer = $('#answer').val(); 
 
    console.log(answer); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='itemList'></div> 
 
<div id='question'></div> 
 
<button onclick='getCount()'>Get Count</button>

関連する問題