简体   繁体   English

jQuery提交前确认

[英]jQuery Confirm Before Submit

<form action ="/submit-page/" method='post' class="editable">
    <fieldset>
    <select name="status" id='status'>
        <option value="Submitted">Submitted</option>
        <option value="Canceled">Canceled</option>
              <option value="Application">Application</option>
    </select>
          <input type="submit" name="submit" value="SAVE">

</form>

I have a form above: When I click on the drop down value "Canceled" and hit the Save button I want to give alert box with a warning message with a Yes and No button. 我上面有一个表单:当我单击下拉值“已取消”并单击“保存”按钮时,我想使用带有“是”和“否”按钮的警告消息给警报框。

If the user cicks on Yes, take him to the submit-page as desired in the form action parameter. 如果用户单击“是”,则根据需要将其带到表单操作参数中的“提交页面”。 if the user clicks No, stay on the same form page without refreshing the page. 如果用户单击“否”,则保留在同一表单页面上而不刷新页面。 Can this be done in jQuery? 可以在jQuery中完成吗?

Hmm... theres is a problem with the other answers on here: they don't work against your HTML. 嗯...这里的其他答案有问题:它们对您的HTML不起作用。

There's a bug in jQuery (I assume it's a bug), where if an element on your form has a name of submit , then triggering the submit event of the form will not work. jQuery中有一个错误(我认为这是一个错误),如果表单上的元素具有name submit ,则触发表单的submit事件将不起作用。

You will need to remove the name attribute from your input type="submit" button or simply give it a name other than "submit". 您将需要从input type="submit"按钮中删除name属性,或者简单地为其指定一个不同于“ submit”的名称。

HTML 的HTML

<form action ="/submit-page/" method='post' class="editable">
  <fieldset>
    <select name="status" id='status'>
      <option value="Submitted">Submitted</option>
      <option value="Canceled">Canceled</option>
      <option value="Application">Application</option>
    </select>
    <input type="submit" value="SAVE" name="submit-button"/>
  </fieldset>
</form>​

jQuery jQuery的

$('#status').on('change', function() {

    var $this = $(this),
        val = $this.val();

    if (val === 'Canceled' && confirm("are you sure?")) {
        $this.closest('form').submit();
    }
});​

PHP 的PHP

$submitted = !empty($_POST['submit-button']);

if($submitted)
{
    // Submit button was pressed.
}
else
{
    // Form was submitted via alternate trigger.
}

Example

Working: http://jsfiddle.net/xixonia/KW5jp/ 工作: http//jsfiddle.net/xixonia/KW5jp/

Not Working: http://jsfiddle.net/xixonia/KW5jp/1/ 不起作用: http : //jsfiddle.net/xixonia/KW5jp/1/

Edit 编辑

You have since updated your question, and this answer is no longer a valid solution for what you are looking for. 此后,您已经更新了问题,此答案不再是您所寻找的有效解决方案。 Instead, look at Chris Platt's answer . 相反,请看克里斯·普拉特Chris Platt)的答案

Edit Again 重新编辑

This is a modified version of Chris Platt's answer . 这是克里斯·普拉特Chris Platt)的答案的修改版本。 It simply waits until the DOM is ready (elements are loaded) before it executes the logic contained within the first $(...) . 它只是等待直到DOM准备就绪(元素已加载),然后才执行第一个$(...)包含的逻辑。

$(function() { // this first jQuery object ensures that...

    /// ... the code inside executes *after* the DOM is ready.

    $('form.editable').submit(function(){
        if ($('#status').val()=='Canceled') {
            if (!confirm('Warning message here. Continue?')) {
                return false;
            }
        }
    });

});
$('form.editable').submit(function(){
    if ($('#status').val()=='Canceled') {
        if (!confirm('Warning message here. Continue?')) {
            return false;
        }
    }
});

yes, it can be done: 是的,可以这样做:

$('#status').change(function(){
   var val = $(this).val();
   if( val == 'Submitted' ) {
     $('.editable').submit();
     return false;
   } else if (val == 'Canceled')
   {
      var answer = confirm('are you sure');
      return false;
   } else {
    ... 
   }
});

This, as opposed to Chris Pratts solution will do it, as soon as you change the selected value in the dropdown box. 与Chris Pratts解决方案相反,此操作将在您更改下拉框中的所选值后立即执行。 His will do it, once you click the submit button. 一旦您单击提交按钮,他的工作便会完成。

The most direct method to intercept the form submission in jQuery is like this: 截取jQuery中表单提交的最直接方法是这样的:

$("form.editable").submit(function(){
    if(confirm("Really submit the form?")) {
        return true;
    } else {
        return false; //form will not be sumitted and page will not reload
    }
});

See http://jqapi.com/#p=submit for more detail. 有关更多详细信息,请参见http://jqapi.com/#p=submit

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

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