-5

我正在尝试将其设为“document.write”,但没有出现。我正在使用 Dreamweaver。

<!DOCTYPE html> 
<html> 
<head> 
<title>Untitled Document</title> 
<script> 
  x=0;
  function newPerson(){
    x++;
    document.write(x);
    var pname = "p" + x;
    var jj =document.forms["frm1"]["fname"].value;
    var gg =document.forms["frm1"]["lname"].value;
    document.write(pname + jj + gg);
  }

</script>
</head>
<body>
  <form name="frm1" onSubmit="newPerson()">

  First Name: <input type="text" id="fname" name="fname"/> <br/>
  Last Name: <input type="text" id="lname" name="lname"/><br/>
  <input type="submit" value="update"/>  </form>

</body> 
</html>
4

4 回答 4

2

现在可以使用

HTML:

<form method="post" name="frm1" onsubmit="return newPerson()"> <!-- return -->
    First Name: <input type="text" id="fname" name="fname"/> <br/>
    Last Name: <input type="text" id="lname" name="lname"/><br/>
    <input type="submit" value="update"/>
</form>

JS:

var x = 0;
function newPerson(){
    var form = document.frm1; // or : document.forms[0]; for first form
    x++;
    var pname = "p" + x;
    var jj = form.elements["fname"].value;
    var gg = form.elements["lname"].value;
    document.write(x);
    document.write(pname + jj + gg);
    return false; // <-- stops submission
}

演示。

于 2013-09-08T05:00:44.363 回答
1

您的代码有一些问题:

  1. submit由于默认操作,表单正在提交。return false;您必须通过添加到您的 JavaScript 函数和return newPerson();表单的onSubmit属性来防止这种情况发生。
  2. 您的document.write(x)行正在覆盖页面,这意味着最初存在的所有元素包括fname并且lname丢失了,因此无法访问。删除此行。
  3. 也代替document.write,outputDiv并将输出写入它。一般来说,这样做document.write被认为是一种不好的做法,因为它会重新绘制整个页面。

纠正上述所有问题,您的代码应该如下所示才能工作。

HTML:

<form name="frm1" onSubmit="return newPerson();">First Name:
    <input type="text" id="fname" name="fname" />
    <br/>Last Name:
    <input type="text" id="lname" name="lname" />
    <br/>
    <input type="submit" value="update" />
</form>
<div id='outputDiv'></div>

JS:

  x = 0;
  function newPerson() {
      x++;
      console.log(x); //just to see what is the value of x in console. not needed otherwise
      var pname = "p" + x;
      var jj = document.frm1.fname.value;
      var gg = document.frm1.lname.value;
      document.getElementById('outputDiv').innerHTML = pname + jj + gg; //set the computed value to the outputDiv.
      return false;
  }

工作演示

于 2013-09-08T05:03:58.830 回答
1

对您的代码进行以下更改。它运行良好:

<script> 
  var x=0;
  function newPerson(){
    x++;
    document.write(x);
    var pname = "p" + x;
    var jj = document.getElementById('fname').value;
    var gg =document.getElementById('lname').value;
    document.write(pname + jj + gg);
  }
</script>

需要什么document.write(x);?你可以很容易地使用alert(x);. 还有一件事,完全不清楚这段代码背后的目的是什么。请确保在将来提供详细的问题。

于 2013-09-08T05:04:33.433 回答
1
TRY THIS to check what's happening:-
<!DOCTYPE html> 
<html> 
<head> 
<title>Untitled Document</title> 
<script> 
x=0;
function newPerson(){
x++;
document.write(x);
  // var pname = "p" + x;
  // var jj =document.forms["frm1"]["fname"].value;
  // var gg =document.forms["frm1"]["lname"].value;
  // document.write(pname + jj + gg); 
return false;
}

</script> </head>

<body>

<form name="frm1" onSubmit="return newPerson()">

First Name: <input type="text" id="fname" name="fname"/> <br/>
Last Name: <input type="text" id="lname" name="lname"/><br/>
<input type="submit" value="update"/>  </form>

</body> </html>

Explanation:- As you submit you form. it calls to your function. document.write executes.
and document.write  will clear document area and will write there value of x.
now when code goes to second line there is no form so there is no value . 

所以这会给出javascript错误。并遵循几点:-

*onsubmit is used with return , means return true then submit form else not. but in your code form will be submitted in any case.

* As form submit to default action, means self page. page will reload. 
you can check what's happening, by putting breakpoints in firebug, or debug console in any modern browser. and please enable persist log upon navigation.

所以你会发现那里发生了什么。

于 2013-09-08T05:04:35.163 回答