-2

我有两个相互镜像的文本框。每当用户输入到文本框 1 时,将自动复制到文本框 2。但是是否可以将其复制到文本框 2 并使用单引号,并且在放置逗号时,另一个值将用单引号关闭。

例如:用户输入(textbox1):苹果

文本框 2(复制值):'apple'

用户输入(textbox1):苹果,橙色

文本框 2(复制值):'apple','orange'

我什至不确定这是否可能,但每一个帮助都将不胜感激。到目前为止,这就是我复制文本框值的方法

function sync()
	{
	  var n1 = document.getElementById('sample1');
	  var n2 = document.getElementById('sample2');
	  n2.value = n1.value;
	}
 Input 1: <input type="text" style= "width: 230px; padding-left: 3px" id="sample1" name="sample1" onkeyup="sync()"> 

<br>
 Copied Input: <input type="text" style= "width: 230px; padding-left: 3px" id="sample2" name="sample2">

4

2 回答 2

1

您可以使用以下函数将变量与输入连接起来:

function sync()
{
var result= '\'' + $('#sample1').val().split(',').join('\',\'') + '\'';
$('#sample2').val(result)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="sample1" onkeyup="sync()">
<input type="text" id="sample2">

于 2019-09-09T07:31:55.613 回答
0

见下面的例子

function sync()
	{
	  var n1 = document.getElementById('sample1');
	  var n2 = document.getElementById('sample2');

     var newval = "'" + n1.value.split( "," ).join( "','" ) + "'";
	  n2.value = newval;
	}
 Input 1: <input type="text" style= "width: 230px; padding-left: 3px" id="sample1" name="sample1" onkeyup="sync()"> 

<br>
 Copied Input: <input type="text" style= "width: 230px; padding-left: 3px" id="sample2" name="sample2">

于 2019-09-09T07:19:47.507 回答