duanlaiquan8174 2016-05-24 05:48
浏览 19
已采纳

如何在自动提交表单javascript中给出名称

This is my code that I am using to submit form with post value

 <form action="<?php echo DOMAIN; ?>contact/booking-form.php" method="post">
  <input type="text" name="name" value="<?php echo $name; ?>" />
    <input type="text" name="email" value="<?php echo $email; ?>" />
    <input type="submit" name="submit" id="submit"  />
    <script>document.getElementById('submit').submit();</script>  
  </form>

Can anybody help me to pass name="submit" value of submit button to another page?

  • 写回答

3条回答 默认 最新

  • douzi7219 2016-05-24 08:00
    关注

    A submit button is only going to be a successful control if it is used to submit the form (and even then only if it has a name and a value … which yours does not).

    If you want submit=submit in your form data when you submit the form with JavaScript, then don't use a submit button to put that data in the form in the first place. Use a hidden input.

    <input type="submit">
    <input type="hidden" name="submit" id="submit" value="submit">
    

    Then you have two other problems.

    First, submit is a method of form elements, not inputs. So you need to change your script to call the right element.

    <script>document.getElementById('submit').form.submit();</script>
    

    Second, if a form has a control called submit then that will clobber the submit method. So you need to get one from a different form (not supported in old versions of Internet Explorer):

    <script>
    var form = document.getElementById('submit').form;
    var submit_method = document.createElement("form").submit;
    submit_method.call(form);
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100