简体   繁体   English

第二个按钮不会在与第一个按钮相同的 div 中生成表单

[英]Second button doesn't produce form in same div as first button

I have a php page with two divs.我有一个带有两个 div 的 php 页面。 In the first div, users click a button and it brings up a form in the second div.在第一个 div 中,用户单击一个按钮,它会在第二个 div 中显示一个表单。 When they submit the form, both divs refresh.当他们提交表单时,两个 div 都会刷新。 All is great.一切都很好。 But I want to add a second button to the first div to bring up a different form in the second div.但我想在第一个 div 中添加第二个按钮,以在第二个 div 中显示不同的表单。 I haven't been able to get this to work, either the first button stops working or the second one doesn't work.我无法让它工作,要么第一个按钮停止工作,要么第二个按钮不起作用。 I tried making the second button a div and calling it by its ID and that didn't work.我尝试将第二个按钮设为 div 并通过其 ID 调用它,但没有成功。 I'm an amateur and don't understand jquery very well (I think this is jquery anyway) and this is just for a project for some friends and I, so any help would be appreciated.我是一个业余爱好者,不太了解 jquery (我认为这是 jquery 无论如何),这只是为一些朋友和我的项目,所以任何帮助将不胜感激。 I may just not be understanding how this works.我可能只是不明白这是如何工作的。

<script>
  $(document).ready(function () {
    $("button").click(function () {
      var handle = "<?php echo $_SESSION['name'] ?>";
      $("#infoblock").load("createhandle.html");
    });
  });
</script>

<div id="div1">
  <button id="reservename">Reserve A Name</button>
</div>

<div id="div2"></div>

I found a solution here: https://stackoverflow.com/a/20074373/9157720我在这里找到了解决方案: https://stackoverflow.com/a/20074373/9157720

It ended up being an issue with syntax and using a pound symbol.它最终成为语法问题并使用井号。 This is the code that worked in case anyone else has this problem.这是在其他人遇到此问题时有效的代码。

<script>
$(document).ready(function(){
                var handle = "<?php echo $_SESSION['name'] ?>";
                $("#reservename").click(function(){
                    $("#infoblock").load( "createhandle.html");
                });
                    $("#uploadpic").click(function(){
                    $("#infoblock").load("uploadpic.html");
                });
                
            });
    </script>

<button id="reservename">Reserve A Name</button>
<button id="uploadpic">Upload A Picture</button>

Consider the following.考虑以下。

<script>
  $(document).ready(function () {
    $("button").click(function () {
      var handle = $(this).data("ref");
      var target = $(this).data("target");
      $(target).load("createhandle.html?handle=" + handle);
    });
  });
</script>

<div id="div1">
  <button data-ref="<?php echo $_SESSION['nameA']; ?>" data-target="#infoblock">Reserve A Name</button>
  <button data-ref="<?php echo $_SESSION['nameB']; ?>" data-target="#div2">Reserve B Name</button>
</div>

<div id="div2">
</div>

Your example is sort of ambiguous, so I wasn't sure what you were trying to accomplish.您的示例有点模棱两可,所以我不确定您要完成什么。 It's best to not mix PHP and HTML if you can.如果可以的话,最好不要混合 PHP 和 HTML。 You can have a reference point in the HTML so that when you need to load something, in relationship to that button, you can pass that into a stand alone PHP Script and get the HTML you need back with .load() .您可以在 HTML 中有一个参考点,这样当您需要加载与该按钮相关的某些内容时,您可以将其传递到独立的 PHP 脚本并获取 Z4C4AD5FCA2E7A3F74DBB1CED00381AA.4Z 您需要的回载.load()

Remember that PHP is executed before the HTML is presented to the Client.请记住,PHP 在 HTML 呈现给客户端之前执行。 So the Session data must be present in PHP Memory when the page is initially loaded.因此,最初加载页面时,Session 数据必须存在于 PHP Memory 中。

Reference:参考:

暂无
暂无

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

相关问题 我的提交表单不是第一次发送,而是在单击提交按钮时第二次发送 - my submit form doesn't send in first time but send in second time when submit button click 删除第二个div中的按钮,它将在第一个div html,jquery中使用相同的id(class)按钮 - remove button from second div which will same id(class) button in first div html,jquery innerHTML 在第一次单击按钮时有效,但在第二次单击时无效 - innerHTML works on first button click but doesn't work on second click jQuery .on适用于第一个html,但不适用于第二个(相同格式) - JQuery .on works on first html but doesn't work on the second(same form) 第二个模式按钮不起作用 - second modal button doesn't work 第二次单击后按钮不起作用 - Button Doesn't Work After Second Click JavaScript按钮在第二次单击时不起作用 - JavaScript button doesn't work on second click 在javascript中的第二个按钮的确认上调用第一个按钮不起作用 - Calling first button on confirm of second button in javascript isn't working WkWebView 消息处理程序第一次不返回文本框的值,但它在我第二次按下按钮时返回 - WkWebView Message handler doesn't return the value of textbox first time, but it does the second time I press the button JQGrid数据不会在第二次或以后的单击事件中重新加载…(它在第一次单击getdata按钮时加载) - JQGrid data doesn't reload on second or subsequent click events…(it loads on first getdata button click)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM