简体   繁体   English

jQuery:转换textarea(用新行分隔)以选择选项菜单

[英]jquery: convert textarea (separated by new lines) to select option menu

this the demo jsfiddle 这是演示jsfiddle

I have tried this : 我已经试过了:

 $('.generate').click(function () { $('.inthis').text('<select class="select" id="' + $('.select-menu').val() + '"><option>' + $('.text-area').val().split('\\n') + '</option><option>' + $('.text-area').val().split('\\n') + '</option><option>' + $('.text-area').val().split('\\n') + '</option></select>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="Text1" type="text" value="option_one" class="select-menu"> <br> <textarea class="text-area" name="Text2" cols="40" rows="5" placeholder="enter one wdg in one line">green yellow blue</textarea> <br> <input class="generate" type="button" value="test" /> <br> <br> <div class="inthis"></div> 

but it did not work. 但它没有用。

I want to be like this : 我想像这样:

<select class="select" id="option_one">
<option>green</option>
<option>yellow</option>
<option>blue</option>
</select>

 $('.generate').click(function() { var options = $('.text-area').val().split('\\n') $('.inthis').html('<select class="select" id="' + $('.select-menu').val() + '"><option>' + options[0] + '</option><option>' + options[1] + '</option><option>' + options[2] + '</option></select>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="Text1" type="text" value="option_one" class="select-menu"> <br> <textarea class="text-area" name="Text2" cols="40" rows="5" placeholder="enter one wdg in one line">green yellow blue</textarea> <br> <input class="generate" type="button" value="test" /> <br> <br> <div class="inthis"></div> 

use .html() not .text() 使用.html()而不是.text()

Firstly, using text() will set the textual value of the element. 首先,使用text()将设置元素的文本值。 If you want to create a new element within it then you can use html() , or append() , or any of jQuery's other DOM manipulation methods. 如果要在其中创建一个新元素,则可以使用html()append() ,或jQuery的任何其他DOM操作方法。 In the example below I used appendTo() so I can maintain a reference to the select that was added. 在下面的示例中,我使用了appendTo()因此我可以维护对添加的select的引用。

From there you can split() the textarea value by each line and then loop through that array to build each lines' value as its own option . 从那里,您可以按每一行split() textarea,然后遍历该数组以建立每行的值作为自己的option Try this: 尝试这个:

 $('.generate').click(function() { var $select = $('<select class="select" id="' + $('.select-menu').val() + '" />').appendTo('.inthis'); var options = $('.text-area').val().split('\\n').map(function(value) { return '<option>' + value + '</option>'; }); $select.html(options.join('')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="Text1" type="text" value="option_one" class="select-menu"> <br> <textarea class="text-area" name="Text2" cols="40" rows="5" placeholder="enter one wdg in one line">green yellow blue</textarea><br> <input class="generate" type="button" value="test" /><br><br> <div class="inthis"></div> 

Text is as the name suggest not working for html or jQuery objects. 文本是名称,建议不适用于html或jQuery对象。

I am using the jQuery syntax on creating the objects and ignoring empty lines in the textarea 我正在使用jQuery语法创建对象并忽略textarea中的空行

 $('.generate').on("click", function() { var $sel = $('<select />', { "class": "select", "id": $('.select-menu').val() }), opts = $('.text-area').val().split('\\n').map( function(val) { if (val) return $('<option/>', { "value": val, "text": val }); }); $sel.append(opts); $('.inthis').html($sel); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="Text1" type="text" value="option_one" class="select-menu"> <br> <textarea class="text-area" name="Text2" cols="40" rows="5" placeholder="enter one wdg in one line">green yellow blue</textarea> <br> <input class="generate" type="button" value="test" /> <br> <br> <div class="inthis"></div> 

Try this one: 试试这个:

 $('.generate').click(function () { var options = $('.text-area').val().split('\\n').map(function(value) { return '<option>' + value + '</option>'; }); $('.inthis').text('<select class="select" id="' + $('.select-menu').val() + '">' + options.join('') + '</select>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="Text1" type="text" value="option_one" class="select-menu"> <br> <textarea class="text-area" name="Text2" cols="40" rows="5" placeholder="enter one wdg in one line">green yellow blue</textarea> <br> <input class="generate" type="button" value="test" /> <br> <br> <div class="inthis"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM