[英]Reading text in textarea line by line
I am using textarea to take user input. 我正在使用textarea接受用户输入。 and want to read line by line.
并想逐行阅读。 But it's not displaying anything I want to make a comma seperated list of the text in different lines
但是它没有显示任何我想在不同行中用逗号分隔的文本列表
JS: JS:
$('input[type=button]').click( function() {
string = document.getElementById("hi").val();
alert(string);
var html="";
var lines = $('#id').val().split('\n');
for(var i = 0;i < lines.length;i++){
//code here using lines[i] which will give you each line
html+=lines[i];
html+=",";
}
$("#inthis").html(string);
});
HTML: HTML:
<textarea id="hi" name="Text1" cols="40" rows="5" placeholder="enter one wdg in one line" ></textarea>
<input type="button" value="test" />
<div id="inthis"></div>
Here is the jsfiddle: 这是jsfiddle:
http://jsfiddle.net/pUeue/1077/ http://jsfiddle.net/pUeue/1077/
Here's updated js... 这是更新的js ...
$('input[type=button]').click(function () {
var html = "";
var lines = $('#hi').val().split('\n');
for (var i = 0; i < lines.length; i++) {
//code here using lines[i] which will give you each line
html += lines[i];
html += ",";
}
html = html.substring(0,html.length-1);
$("#inthis").html(html);
});
Firstly you have a confused mix of native javascript and jQuery code in your example. 首先,在示例中,您将本机javascript和jQuery代码混为一谈。 A native DOM element has no
val()
method for example, that's jQuery. 原生DOM元素没有
val()
方法,例如jQuery。 Secondly, you could massively simplify your code by just using split()
and join(',')
. 其次,只需使用
split()
和join(',')
即可大大简化代码。 Try this: 尝试这个:
$('input[type=button]').click( function() {
var string = $("#hi").val().split('\n').join(',');
$("#inthis").html(string);
});
.val()
is a Jquery function, you will have to convert the DOM element to jquery obj or use .value
.val()
是一个Jquery函数,您必须将DOM元素转换为jquery obj或使用.value
$('input[type=button]').click(function () {
string = $(document.getElementById("hi")).val();
string = string.replace("\n", ",")
$("#inthis").html(string);
});
http://jsfiddle.net/pUeue/1090/ http://jsfiddle.net/pUeue/1090/
there is mistake in second line u have used jQuery val instead of value so try below:- 在第二行中有错误,您使用了jQuery val而不是value,所以请尝试以下操作:
string = document.getElementById("hi").value;
and need to put the below line before you load the string in div.:- 并且需要在将字符串加载到div中之前放置以下行:-
html = html.substring(0,html.length-1);
http://jsfiddle.net/pUeue/1085/ http://jsfiddle.net/pUeue/1085/
$('input[type=button]').click( function() {
string = $(document.getElementById("hi")).val();
alert(string);
var htmlD="";
var lines = string.split('\n');
for(var i = 0;i < lines.length;i++){
//code here using lines[i] which will give you each line
htmlD+=lines[i];
htmlD+=",";
}
$("#inthis").html(htmlD);
});
I have corrected your code here: http://jsfiddle.net/pUeue/1080/ . 我已经在这里更正了您的代码: http : //jsfiddle.net/pUeue/1080/ 。
$('input[type=button]').click( function() {
string = $("#hi").val();
alert(string);
var html="";
var lines = $('#hi').val().split('\n');
for(var i = 0;i < lines.length;i++) {
//code here using lines[i] which will give you each line
html+=lines[i];
html+=",";
}
$("#inthis").html(html);
});
try this 尝试这个
$('input[type=button]').click( function() {
var lines = $('#hi').val().split(/\n/);
$("#inthis").html(lines.join(","));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.