简体   繁体   English

同一代码在同一页面上的工作方式不同

[英]Same code working differently on the same page

I hope you're doing great. 希望您一切顺利。

I'm having a hard time figuring this out. 我很难解决这个问题。 I'm using the same code for an agree to terms box and submit button but with different button images and links. 我对“同意条款”框和“提交”按钮使用了相同的代码,但是按钮和按钮的图像和链接不同。 I just copied the code for them to work in the same way. 我只是复制了代码,让它们以相同的方式工作。 The buttons are on the same page but take you to different checkout pages. 这些按钮在同一页面上,但带您进入不同的结帐页面。 The first code is working fine, it blocks the user to proceed if they don't check the agreement box but the second code even though it's the same code, it's not doing the job; 第一个代码运行良好,如果用户未选中协议框,它将阻止用户继续操作,但是第二个代码即使是相同的代码,也无法完成工作; it's letting people through without agreeing to the terms. 它使人们不同意条款就可以通过。

Below are both codes and you can see they're different because of their hyperlinks and button images. 以下是这两个代码,由于它们的超链接和按钮图像,您可以看到它们有所不同。

This a great community. 这是一个伟大的社区。 I'm a beginner, excuse me if it's an amateur question. 我是一个初学者,请问这是一个业余问题。 I really hope you all can help me, I really appreciate it! 我真的希望大家能帮助我,我真的很感激!

Thank you so much! 非常感谢!

 <html> <head> <script> function disableSubmit() { document.getElementById("submit").disabled = true; } function activateButton(element) { if(element.checked) { document.getElementById("submit").disabled = false; } else { document.getElementById("submit").disabled = true; } } </script> <center><button type="submit" id="submit" onclick="document.location.href='https://www.jvzoo.com/b/570‌​51/250513/13';"><img src="http://i.jvzoo.com/57051/250513/13" alt="Tube Cash Blueprint (2M)" border="0" alt="Submit"/></button></center> </head> <center><h6> Tube Cash Blueprint Agreement</h6></center> <body onload="disableSubmit()"> <center><input type="checkbox" name="terms" id="terms" onchange="activateButton(this)"/> Check here to indicate that you have read and agreed to the terms of the <a target="_blank" href="https://johnmichaelmarketing.com/tbc-agreement/">Tube Cash Blueprint Costumer Agreement</a> </center> <br/><br/> </body> </html> 
 <html> <head> <script> function disableSubmit() { document.getElementById("submit").disabled = true; } function activateButton(element) { if(element.checked) { document.getElementById("submit").disabled = false; } else { document.getElementById("submit").disabled = true; } } </script> <center><button type="submit" id="submit" onclick="document.location.href='https://www.jvzoo.com/b/57051/250467/13';"><img src="http://i.jvzoo.com/57051/250467/13" alt="Tube Cash Blueprint" border="0" alt="Submit"/></button></center> </head> <center><h6> Tube Cash Blueprint Agreement</h6></center> <body onload="disableSubmit()"> <center><input type="checkbox" name="terms" id="terms" onchange="activateButton(this)"/> Check here to indicate that you have read and agreed to the terms of the <a target="_blank" href="https://johnmichaelmarketing.com/tbc-agreement/">Tube Cash Blueprint Costumer Agreement</a> </center> <br/><br/> </body> </html> 

You cannot use the same id for 2 elements. 您不能对2个元素使用相同的ID。

This id: 这个ID:

<button type="submit" id="submit" onclick="document.location.href='https://www.jvzoo.com/b/57051/250467/13';" disabled=""><img src="https://i.jvzoo.com/57051/250467/13" alt="Tube Cash Blueprint" border="0"></button>

is clashing with this: 正在与此冲突:

<button type="submit" id="submit" onclick="document.location.href='https://www.jvzoo.com/b/570&zwnj;&#8203;51/250513/13';"><img src="https://i.jvzoo.com/57051/250513/13" alt="Tube Cash Blueprint (2M)" border="0"></button>

Just change the ids and it will work. 只需更改ID,它就会起作用。 Now you have to write a different function for each id, or use a name instead to be able to use them in both 现在,您必须为每个id编写一个不同的函数,或者改用一个name才能在两个id中使用它们

Quick and dirty solution - copy functions, change id: 快速又肮脏的解决方案-复制功能,更改ID:

<script>
  function disableSubmit1() {
      document.getElementById("submit1").disabled = true;
  }

  function activateButton1(element) {
      if(element.checked) {
          document.getElementById("submit1").disabled = false;
      }
      else  {
          document.getElementById("submit1").disabled = true;
      }
  }

  function disableSubmit2() {
      document.getElementById("submit2").disabled = true;
  }

  function activateButton2(element) {
      if(element.checked) {
          document.getElementById("submit2").disabled = false;
      }
      else  {
          document.getElementById("submit2").disabled = true;
     }
  }
</script>

<!-- button 1 -->

<button type="submit" id="submit1" disabled="" onclick="alert('submission1')">B1</button>

<input type="checkbox" name="terms" id="terms1" onchange="activateButton1(this)" /> 


<!-- button 2 -->

<button type="submit" id="submit2" disabled="" onclick="alert('submission2')">B2</button>

<input type="checkbox" name="terms" id="terms2" onchange="activateButton2(this)" /> 

You have two html tag in one document. 一个文档中有两个html标记。 When i trace your code with my firefox inspector you second code are as an srcipt of the first one. 当我用firefox检查器跟踪您的代码时,您的第二个代码就是第一个的代码。 What i saw and copy: 我看到并复制的内容:

<br><br>

<script type="text/javascript">
    <html>
 <head>
 <script>
 function disableSubmit() {
 document.getElementById("submit").disabled = true;
 }

 function activateButton(element) {

  if(element.checked) {
    document.getElementById("submit").disabled = false;
   }
   else  {
    document.getElementById("submit").disabled = true;
  }

 }
</script>

If you want your code work remove <html><head><script> from the first of second code. 如果您希望代码工作,请从第二个代码的第一个中删除<html><head><script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM