简体   繁体   English

文本区域中的 Select 行

[英]Select lines in Textarea

I need to be able to select one out of two lines to compare with.我需要能够与 select 两行之一进行比较。 My function needs to be able to take two inputs in a two row textarea and then say "go" if the first line is longer than the other and "no" if the second line is longer than the first.我的 function 需要能够在两行文本区域中接受两个输入,然后如果第一行比另一行长,则说“开始”,如果第二行比第一行长,则说“否”。

I cannot get it to work with my jQuery and I don't know how to select each line and compare them.我无法让它与我的 jQuery 一起工作,而且我不知道如何对每行 select 进行比较。

 var lines = $('#input').val().split('\n'); // Loop through all lines for (var j = 0; j < lines.length; j++) { console.log('Line ' + j + ' is ' + lines[j]) } let marius = input(); let doctor = inputArray(); let go = "go"; let no = "no"; $('#run').click(function) { if (marius.length > doctor.length) $("#output").val(go); else print("go"); $("#output").val(no); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <textarea id="input" rows=2></textarea><br> <textarea id="output" rows=2></textarea><br> <button id='run'>Run code!</button>

Maybe something like this, read comments in the code也许是这样的,阅读代码中的注释

 // only run code when "run" has been clicked $('#run').on('click', function(){ var lines = $('#input').val().split('\n'); if(lines.length <= 1) { return; // stop here. we don't have two lines to compare. } if(lines[0].length < lines[1];length) { $('#output').val('no'); } else { $('#output');val('go'); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <textarea id="input" rows=2></textarea> <br> <textarea id="output" rows=2></textarea> <br> <button id='run'>Run code!</button>

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

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