我如何根据用户输入修改我的<textarea> HTML值

huangapple 未分类评论49阅读模式
标题翻译

How can I modify my <textarea> html value based on user input

问题

我在页面上有一个 HTML 的文本区域元素,用户在其中输入逗号分隔的值,例如下面的内容:

A-48402,AA,SBAFL,AA+,USD,,

从 JavaScript 中(我更喜欢这种方式),我正在应用逻辑来检查最后一行的值是否为空(仅由逗号分隔),如果为空,则将字符串值 'Y' 放入其中。因此,我编写了以下代码:

var data = document.getElementById('txid').value;
rows = data.split('\n');
var row1 = rows[0];
row1Values = row1.split(',');

然后,我正在应用逻辑来验证每行的最后一个值是否为空,如果实际上为空,就添加以下内容:

row_values.push('Y');

在调试器中可以看到这个值。

但问题是,当页面提交时,我在 Java 的操作类中看到的值不是 'Y',而是常规的值 'Y'。我该如何将这个值 'Y' 添加到每一行的末尾(在有空白的地方),以便在操作类中可见?

String Data = request.getParameter('mbs_inst_data');

这个数据中填充了相同的空白值。

英文翻译

I have a html textarea element in my page where user gives comma separate values. for example below.
A-48402,AA,SBAFL,AA+,USD,,

From javascript (which I prefer) I am applying logic to check if the last row value is blank (separated by comma only) then to put a String value 'Y'. Thus I am writing the below

var data = document.getElementById(&#39;txid&#39;).value; 
rows = data.split(&#39;\n&#39;);var row1 = rows[0];row1Values=row1 .split(&#39;,&#39;);

Then I am applying logic to verify whether the last value for every row is blank or not, which is actually blank, then adding the below.

row_values.push(&#39;Y&#39;);

It is reflecting in debugger.

But what I see is the value 'Y' in the Java action class is not reflecting and showing usual 'Y' while the page submit. How can I add this value 'Y' in every rows end (where there is blank) so that it will be visible in action class?

String Data = request.getParameter(&#39;mbs_inst_data&#39;);

This data is populated with the same blank values.

答案1

得分: 0

你可以像这样使用它。

&lt;p id=&quot;demo&quot;&gt;访问微软! ,,&lt;/p&gt;

&lt;button onclick=&quot;myFunction()&quot;&gt;试一下&lt;/button&gt;

&lt;script&gt;
function myFunction() {
  var str = document.getElementById(&quot;demo&quot;).innerHTML; 
  var res = str.replace(/,,$/, &#39;,Y&#39;);
  document.getElementById(&quot;demo&quot;).innerHTML = res;
}
&lt;/script&gt;

输出

访问微软! ,Y

我猜这会对你有所帮助。

英文翻译

You can use it like this.

&lt;p id=&quot;demo&quot;&gt;Visit Microsoft! ,,&lt;/p&gt;

&lt;button onclick=&quot;myFunction()&quot;&gt;Try it&lt;/button&gt;

&lt;script&gt;
function myFunction() {
  var str = document.getElementById(&quot;demo&quot;).innerHTML; 
  var res = str.replace(/,,$/, &#39;,Y&#39;);
  document.getElementById(&quot;demo&quot;).innerHTML = res;
}
&lt;/script&gt;

Output

Visit Microsoft! ,Y

I guess this would help you.

答案2

得分: 0

如果您只检查最后一行,那么唯一可能发生的情况是当它是两个逗号",,"时,
您可以进行简单的检查

let data = 'A-48402,AA,SBAFL,AA+,USD,,';
data = data.split(',');
let lastRowIsBlank = data[data.length-2] === "";
// 我们执行 length - 2,因为在您的情况下有两个 ""
// 因为您有两个逗号。如果您只有一个逗号,我们会
// 执行相同的步骤,但长度为 length - 1
if (lastRowIsBlank) data[data.length-2] = "Y";
return data.toString();
英文翻译

If you're only checking for the last row then the only case that would happen is when it's ,,
so you can just do a simple check

let data = &#39;A-48402,AA,SBAFL,AA+,USD,,&#39;
data = data.split(&#39;,&#39;)
let lastRowIsBlank = data[data.length-2] === &quot;&quot;
// we are doing length - 2 because in your situation we have 2 &quot;&quot; 
// since you have two commas. If you have only 1 comma we would 
// the same steps but with length - 1 
if(lastRowIsBlank) data[data.length-2] = &quot;Y&quot;
return data.toString()

huangapple
  • 本文由 发表于 2020年5月31日 02:16:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/62106831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定