3

I am using textarea to take user input. 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:

$('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:

<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:

http://jsfiddle.net/pUeue/1077/

4

8 回答 8

5

这是更新的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);
});
于 2013-10-17T09:26:40.460 回答
1

首先,您的示例中混杂了原生 javascript 和 jQuery 代码。原生 DOM 元素没有val()方法,例如 jQuery。其次,您只需使用split()and就可以大大简化您的代码join(',')。尝试这个:

$('input[type=button]').click( function() {
    var string = $("#hi").val().split('\n').join(',');
    $("#inthis").html(string);
});

示例小提琴

于 2013-10-17T09:26:24.847 回答
0

第二行有错误你使用了 jQuery val 而不是 value 所以试试下面: -

string = document.getElementById("hi").value;

并且需要在将字符串加载到 div 之前放置以下行:-

html = html.substring(0,html.length-1);
于 2013-10-17T09:26:33.240 回答
0

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);
});
于 2013-10-17T09:27:26.913 回答
0

我在这里更正了您的代码: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);
});
于 2013-10-17T09:27:38.393 回答
0

.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/

于 2013-10-17T09:24:37.760 回答
0

尝试这个

$('input[type=button]').click( function() {
    var lines =  $('#hi').val().split(/\n/);
    $("#inthis").html(lines.join(","));
});
于 2013-10-17T09:31:37.673 回答
0

改变:

string = document.getElementById("hi").val();

string = document.getElementById("hi").value;

jsfiddle

于 2013-10-17T09:25:00.693 回答