清除文本字段中的值并在其他文本字段中填充该值?

时间:2014-12-30 09:54:29

标签: javascript pdf if-statement

我有以下代码:

var s1 = this.getField("One").value;
var s2 = this.getField("Two").value;
var s3 = this.getField("Three").value;
var s1n = this.getField("One");
var s2n = this.getField("Two");
var s3n = this.getField("Three");

if(s1.length > 0) & (s2.length == 0) & (s3.length == 0){
  s1n = '' & s2n = s1 & s3n = ''
  else if (s1.length > 0) & (s2.length > 0) & (s3.length == 0){
          s1n = s1 & s2n = '' & s3n = s3
          else if (s1.length > 0) & (s2.length > 0) & (s3.length > 0){
          s1n = s1 & s2n = s2 & s3n = s3
}}}

我连续三个文本字段。 如果我填写" One"只有我想清除" One"并在" Two"中填写该值。 如果我填写" One"和"两个"我想清除" Two"并在" Three"中填写该值。 如果我填写" One"和"两个"和"三"我什么都不想做 但是上面的代码我得到一个语法错误。要做到这一点,我必须做些什么?

1 个答案:

答案 0 :(得分:0)

以下代码应该可以使用



function check() {
  var s1 = document.getElementById("ip1").value;
  var s2 = document.getElementById("ip2").value;
  var s3 = document.getElementById("ip3").value;

  var s1n = document.getElementById("ip1");
  var s2n = document.getElementById("ip2");
  var s3n = document.getElementById("ip3");

  if ((s1.length > 0) && (s2.length == 0) && (s3.length == 0)) {
    s2n.value = s1n.value;
    s1n.value = "";
  }

  if ((s1.length > 0) && (s2.length > 0) && (s3.length == 0)) {
    s3n.value = s2n.value;
    s2n.value = "";
  }
}

<input type="text" id="ip1" placeholder="Enter value 1">
<input type="text" id="ip2" placeholder="Enter value 2">
<input type="text" id="ip3" placeholder="Enter value 3">
<input type="button" value="submit" onclick="check()">
&#13;
&#13;
&#13;