jQuery代码在Chrome中工作-不在Safari中工作

jQuery代码在Chrome中工作-不在Safari中工作,jquery,safari,Jquery,Safari,我在Safari中的jQuery(jQuery-1.12.4)代码有问题,但在Chrome中没有。提交表单时,使用Safari时会调用切换默认操作。该代码在Chrome中正常工作。开发人员控制台中没有出现错误,只有注意:未检测到提交操作已记录 $("form").submit(function (event) { // check acal-position-container for children if($('.acal-position-container').has('div'

我在Safari中的jQuery(jQuery-1.12.4)代码有问题,但在Chrome中没有。提交表单时,使用Safari时会调用切换默认操作。该代码在Chrome中正常工作。开发人员控制台中没有出现错误,只有
注意:未检测到提交操作已记录

$("form").submit(function (event) {
  // check acal-position-container for children
  if($('.acal-position-container').has('div').length < 1){
    alert("You cannot submit an empty form.\n\nIf you want to delete the current day, use the Delete button without removing any current positions.");
    return false;
  }

  // create the form_action post variable to decide what post action will be executed on the server
  var form_action = $("<input>").attr("type", "hidden").attr("name", "submit_type");
  // unset any old action properties (in case of a cancelled user action)
  $('form').append($(form_action).val(''));
  // confirm various actions
  switch ( $('button:focus').val() ) {
    case 'delete':
      $('form').append($(form_action).val('delete'));
      return confirm("Delete all positions for this day?");
    case 'publish':
      $('form').append($(form_action).val('publish'));
      return confirm("Publish all positions for this day?");
    case 'draft':
      $('form').append($(form_action).val('draft'));
      return confirm("Save all positions for this day as a draft?");
    default:
      console.log('Notice: No submit action detected!');
  }

});
$(“表单”).submit(函数(事件){
//检查儿童的acal位置容器
if($('.acal position container')。具有('div')。长度<1){
警报(“您不能提交空表单。\n\n如果要删除当天,请使用“删除”按钮,而不删除任何当前位置。”);
返回false;
}
//创建form_action post变量以决定将在服务器上执行的post操作
var form_action=$(“”).attr(“type”,“hidden”).attr(“name”,“submit_type”);
//取消设置任何旧操作属性(在取消用户操作的情况下)
$('form').append($(form_action).val('');
//确认各种行动
开关($('button:focus').val(){
案例“删除”:
$('form').append($(form_action.val('delete'));
返回确认(“删除当天的所有职位?”);
“发布”案例:
$('form').append($(form_action.val('publish'));
返回确认(“发布当天的所有职位?”);
“草案”一案:
$('form').append($(form_action.val('draft'));
返回确认(“将当天的所有头寸保存为草稿?”);
违约:
log('注意:未检测到提交操作!');
}
});
html

<button name="submit" type="submit" value="draft" class="acal-confirm" title="Do not make available publically">Save draft</button>
<button name="submit" type="submit" value="publish" class="acal-confirm" title="Save and publish the positions publically">Publish</button>
<button name="submit" type="submit" value="delete" class="acal-confirm" title="Delete the positions for this day">Delete day</button>
保存草稿
发表
删除日期
知道问题出在哪里吗

谢谢

编辑:根据要求,更多html

<form action="http://acal.dev/acal/calendar/project_positions/update_position/2/2" role="form" method="post" accept-charset="utf-8">
<input type="hidden" name="running_day" value="1" style="display:none;" />
<input type="hidden" name="project_id" value="2" style="display:none;" />

<div class="form-inline pos-control-menu cal-card-hover">
  <h2>Project: Test</h2>
  <div class="form-group">
    <label style="margin-right:10px;">Select day</label>
    <select  id="select_day" style="margin-right:10px;" class="form-control input-lg">
        <option value="1">Monday</option>
        <option value="2">Tuesday</option>
        <option value="3">Wednesday</option>
        <option value="4">Thursday</option>
        <option value="5">Friday</option>
        <option value="6">Saturday</option>
        <option value="0">Sunday</option>
    </select>
  </div>
  <!-- Action buttons -->
  <button name="submit" type="submit" value="draft" class="btn btn-primary btn-lg acal-confirm" title="Do not make available publically">Save draft</button>
  <button name="submit" type="submit" value="publish" style="margin-left:10px;" class="btn btn-primary btn-lg acal-confirm" title="Save and publish the positions publically">Publish</button>
  <button name="submit" type="submit" value="delete" style="margin-left:10px;" class="btn btn-primary btn-lg acal-confirm" title="Delete the positions for this day">Delete day</button>
</div>

<!-- POSITION CARD CONTAINER -->
<div class="acal-position-container">
    <div class="acal-card-container">

  <input type="hidden" name="id[]" value="5">
  <div class="acal-card cal-card-hover">
    <div style="text-align:right;margin-bottom:10px;">
      <a href="javascript:;" class="remove-position-card"><strong>Remove position </strong><span class="glyphicon glyphicon-remove"></span></a>
    </div>
    <div class="row">
      <div class="form-group col-sm-6">
        <label for="title[]">Title (required)</label><input type="text" name="title[]" value="Monday&#039;s steps"  class="form-control" required />
      </div>
      <div class="form-group col-sm-6">
        <label for="max_vol[]">Maximum participants needed</label><input type="number" name="max_vol[]" value="2" min="1" max="999" class="form-control"  />
      </div>
    </div>
    <div class="form-group">
      <label for="description[]">Description</label><textarea name="description[]" cols="40" rows="10"  style="height:100px;" class="form-control">The description...</textarea>
    </div>
  </div>

</div>
</div>

<input type="button" id="add_position_card" style="margin-top:15px;" class="btn btn-success btn-lg btn-block" value="Add position">
</form>  

项目:测试
选择日期
星期一
星期二
星期三
星期四
星期五
星期六
星期日
保存草稿
发表
删除日期
标题(必选)
所需最大参与人数
描述描述。。。

感谢@vi5ion提出在“提交”按钮上调用函数的想法。这是我使用的代码

/* Submit form event 
---------------------------------------------------------------------------- */
$('button[type="submit"]').click(function() {
    // check if any positions exist (ie, user has not removed all the positions)
    $("form").submit(function (event) {
        // check acal-position-container for children
        if($('.acal-position-container').has('div').length < 1){
            alert("You cannot submit an empty form.\n\nIf you want to delete the current day, use the Delete button without removing any current positions.");
            //event.preventDefault();
            return false;
        }
    });

    // create the form_action post variable to decide what post action will be executed on the server
    var form_action = $("<input>").attr("type", "hidden").attr("name", "submit_type");
    // unset any old action properties (in case of a cancelled user action)
    $('form').append($(form_action).val(''));
    // confirm various actions
    switch ( this.value ) {
        case 'delete':
            $('form').append($(form_action).val('delete'));
            return confirm("Delete all positions for this day?");
        case 'publish':
            $('form').append($(form_action).val('publish'));
            return confirm("Publish all positions for this day?");
        case 'draft':
            $('form').append($(form_action).val('draft'));
            return confirm("Save all positions for this day as a draft?");
        default:
            console.log('Notice: No submit action detected!');
    }

});
/*提交表单事件
---------------------------------------------------------------------------- */
$('button[type=“submit”]”)。单击(函数(){
//检查是否存在任何位置(即,用户未删除所有位置)
$(“表格”)。提交(功能(事件){
//检查儿童的acal位置容器
if($('.acal position container')。具有('div')。长度<1){
警报(“您不能提交空表单。\n\n如果要删除当天,请使用“删除”按钮,而不删除任何当前位置。”);
//event.preventDefault();
返回false;
}
});
//创建form_action post变量以决定将在服务器上执行的post操作
var form_action=$(“”).attr(“type”,“hidden”).attr(“name”,“submit_type”);
//取消设置任何旧操作属性(在取消用户操作的情况下)
$('form').append($(form_action).val('');
//确认各种行动
开关(此值){
案例“删除”:
$('form').append($(form_action.val('delete'));
返回确认(“删除当天的所有职位?”);
“发布”案例:
$('form').append($(form_action.val('publish'));
返回确认(“发布当天的所有职位?”);
“草案”一案:
$('form').append($(form_action.val('draft'));
返回确认(“将当天的所有头寸保存为草稿?”);
违约:
log('注意:未检测到提交操作!');
}
});

感谢@vi5ion提出在“提交”按钮上调用函数的想法。这是我使用的代码

/* Submit form event 
---------------------------------------------------------------------------- */
$('button[type="submit"]').click(function() {
    // check if any positions exist (ie, user has not removed all the positions)
    $("form").submit(function (event) {
        // check acal-position-container for children
        if($('.acal-position-container').has('div').length < 1){
            alert("You cannot submit an empty form.\n\nIf you want to delete the current day, use the Delete button without removing any current positions.");
            //event.preventDefault();
            return false;
        }
    });

    // create the form_action post variable to decide what post action will be executed on the server
    var form_action = $("<input>").attr("type", "hidden").attr("name", "submit_type");
    // unset any old action properties (in case of a cancelled user action)
    $('form').append($(form_action).val(''));
    // confirm various actions
    switch ( this.value ) {
        case 'delete':
            $('form').append($(form_action).val('delete'));
            return confirm("Delete all positions for this day?");
        case 'publish':
            $('form').append($(form_action).val('publish'));
            return confirm("Publish all positions for this day?");
        case 'draft':
            $('form').append($(form_action).val('draft'));
            return confirm("Save all positions for this day as a draft?");
        default:
            console.log('Notice: No submit action detected!');
    }

});
/*提交表单事件
---------------------------------------------------------------------------- */
$('button[type=“submit”]”)。单击(函数(){
//检查是否存在任何位置(即,用户未删除所有位置)
$(“表格”)。提交(功能(事件){
//检查儿童的acal位置容器
if($('.acal position container')。具有('div')。长度<1){
警报(“您不能提交空表单。\n\n如果要删除当天,请使用“删除”按钮,而不删除任何当前位置。”);
//event.preventDefault();
返回false;
}
});
//创建form_action post变量以决定将在服务器上执行的post操作
var form_action=$(“”).attr(“type”,“hidden”).attr(“name”,“submit_type”);
//取消设置任何旧操作属性(在取消用户操作的情况下)
$('form').append($(form_action).val('');
//确认各种行动
开关(此值){
案例“删除”:
$('form').append($(form_action.val('delete'));
返回确认(“删除当天的所有职位?”);
“发布”案例:
$('form').append($(form_action.val('publish'));
返回确认(“发布当天的所有职位?”);
“草案”一案:
$('form').append($(form_action.val('draft'));
返回确认(“将当天的所有头寸保存为草稿?”);
违约:
log('注意:未检测到提交操作!');
}
});

能否显示更多html?我的第一个猜测是,无论出于何种原因,按钮都没有焦点。您是否检查过Safari中是否返回任何内容?您是对的,在控制台中运行
$('button:focus')
在Safari中返回
[]
,在Chrome中返回
[prevObject:jQuery.fn.init(1),上下文:文档,选择器:“button:focus”
。那么为什么会发生这种情况呢?也许调用submit会将焦点移到在特定浏览器中提交的表单上。又是猜猜看