标题翻译
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('txid').value;
rows = data.split('\n');var row1 = rows[0];row1Values=row1 .split(',');
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('Y');
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('mbs_inst_data');
This data is populated with the same blank values.
答案1
得分: 0
你可以像这样使用它。
<p id="demo">访问微软! ,,</p>
<button onclick="myFunction()">试一下</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/,,$/, ',Y');
document.getElementById("demo").innerHTML = res;
}
</script>
输出
访问微软! ,Y
我猜这会对你有所帮助。
英文翻译
You can use it like this.
<p id="demo">Visit Microsoft! ,,</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/,,$/, ',Y');
document.getElementById("demo").innerHTML = res;
}
</script>
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 = 'A-48402,AA,SBAFL,AA+,USD,,'
data = data.split(',')
let lastRowIsBlank = data[data.length-2] === ""
// we are doing length - 2 because in your situation we have 2 ""
// 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] = "Y"
return data.toString()
专注分享java语言的经验与见解,让所有开发者获益!
评论