doutan2111 2014-10-23 09:31
浏览 62

无法获取使用jQuery Popconfirm插件单击的按钮的名称

So I decided to use this plugin https://github.com/Ifnot/PopConfirm as it was recommended as a good confirm button. I have it set up and all working well, except for one thing.

I validate all my forms in PHP using the name of the button clicked since I have several buttons such as save, cancel, delete. Therefore when a form is submitted in PHP the POST variables available to me are these below and I can validate the button using

PHP e.g. 
Array ( [supplier] => "david" [customer] => "new buy" [save] => )

Validate Using
if(isset($_POST['save'])){} 

But when I use the popconfirm plugin, and since it uses the .submit() to submit the form, on form submission I only get the values of the input box but not the name of the button

Using popconfirm to submit the form, I end up with
Array ( [supplier] => "david" [customer] => "new buy" ) 

Since the $_POST['save'] is missing when using popconfirm, I therefore can not validate which button was pressed in PHP as it is not passed on.

I can see the plugin uses

form.submit(); 

to submit the form, but what I can not figure out is why is the name of the button not also being posted like how PHP does it or how to get the submit function to also pass on the button name variable

  • 写回答

2条回答 默认 最新

  • douchen4547 2014-11-23 20:58
    关注

    When you submit a form using jQuery (which PopConfirm uses), buttons' names and values are not passed with other form data (and I don't know why exactly).
    If you see the source of jquery.popconfirm.js you can search for the part that handles buttons by searching for this line of comment:

        // If the button is a submit one
    

    You'll find the part that handles buttons. Add this lines of code right before form.submit();

    btnName = typeof(self.attr('name')) === 'undefined' ? '' : self.attr('name');
    btnVal = typeof(self.attr('value')) === 'undefined' ? '' : self.val();
    form.append('<input type="hidden" name="' + btnName + '" value="' + btnVal + '" />');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?