Can thinkPHP have two identical forms on the same page?

WBOY
Release: 2023-03-01 17:42:01
Original
1377 people have browsed it

I submitted it using ajax. Why can I submit data in the first form but not the second one? There is no response after clicking submit and no error is reported in the console. The two forms are exactly the same. Why is this?

<code>var oBtn1=document.getElementById('btn1');
var oTxt=document.getElementById('textarea');
oBtn1.onclick=function(){
  oV1=oTxt.value;
  ajax("{:U('zhuye/zhuye')}",oV1,function(str){
    if(str==1){
      window.location.reload();
    }
  });
}
<form>
   <textarea id="textarea" rows="1"></textarea>
   <input type="button" id="btn1" value="提交">
</form>
<form>
   <textarea id="textarea" rows="1"></textarea>
   <input type="button" id="btn1" value="提交">
</form></code>
Copy after login
Copy after login

Reply content:

I submitted it using ajax. Why can I submit data in the first form but not the second one? There is no response after clicking submit and no error is reported in the console. The two forms are exactly the same. Why is this?

<code>var oBtn1=document.getElementById('btn1');
var oTxt=document.getElementById('textarea');
oBtn1.onclick=function(){
  oV1=oTxt.value;
  ajax("{:U('zhuye/zhuye')}",oV1,function(str){
    if(str==1){
      window.location.reload();
    }
  });
}
<form>
   <textarea id="textarea" rows="1"></textarea>
   <input type="button" id="btn1" value="提交">
</form>
<form>
   <textarea id="textarea" rows="1"></textarea>
   <input type="button" id="btn1" value="提交">
</form></code>
Copy after login
Copy after login

getElementById will not continue execution if it finds the first element that meets the conditions, so if your two IDs are the same, you can only get the content of the first form

Isn’t the ID of your textarea duplicated?

This is an obvious mistake. The ID of elements in HTML should be unique.

ID is unique and cannot be repeated.

id is unique on the same page

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!