jQuery在Chrome中可以立即工作,但在Firefox中需要页面刷新
我正在使用Rails4.1.5和jQuery1.10.2。我有三个包含代码的js文件,它们在Chrome和Firefox中都可以正常工作。但是为了让jQuery在Firefox中工作,我必须刷新页面一次;在不刷新页面的情况下访问该页面会生成一个jQuery较少的页面 application.js:jQuery在Chrome中可以立即工作,但在Firefox中需要页面刷新,jquery,ruby-on-rails,google-chrome,firefox,Jquery,Ruby On Rails,Google Chrome,Firefox,我正在使用Rails4.1.5和jQuery1.10.2。我有三个包含代码的js文件,它们在Chrome和Firefox中都可以正常工作。但是为了让jQuery在Firefox中工作,我必须刷新页面一次;在不刷新页面的情况下访问该页面会生成一个jQuery较少的页面 application.js: // This is a manifest file that'll be compiled into application.js, which will include all the files
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require parsley
//= require moment
//= require bootstrap
//= require bootstrap-datetimepicker
//= require turbolinks
//= require_tree .
_application.html.erb:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Allow mobile scrolling. -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title><%= full_site_title %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application",
"data-turbolinks-track" => true %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tags %>
<%= favicon_link_tag %>
</head>
...
quarters.js:
$(document).ready(function(event){
$('.datetimepicker').datetimepicker({
format: 'YYYY-MM-DD HH:mm:00',
useSeconds: false,
useCurrent: false,
minuteStepping: 15,
language: 'en'
});
// Only the new form has a div with this class, so we check whether the
// class exists before grabbing the data. Otherwise, we'd get an error.
if ($('.deadlines_class').length) {
deadlines = $('.deadlines_class').data('deadlines');
$('#quarter_start_date').val(deadlines["start"]);
$('#quarter_project_proposal_deadline').val(deadlines["proposal"]);
$('#quarter_student_submission_deadline').val(deadlines["submission"]);
$('#quarter_advisor_decision_deadline').val(deadlines["decision"]);
$('#quarter_admin_publish_deadline').val(deadlines["admin"]);
$('#quarter_end_date').val(deadlines["end"]);
};
$('#quarter-form-tooltip').tooltip({
container: 'body',
trigger: 'hover'
});
});
evaluations.js:
$(document).ready(function(event){
function checkRadio(event) {
if ($('#new-eval-question #_question_type').val() == 'Radio button') {
$('#new-eval-question #radio-button-group').show();
} else {
$('#new-eval-question #radio-button-group').hide();
};
};
checkRadio();
$('#new-eval-question #_question_type').on('change', checkRadio);
var rbDiv = $('#new-eval-question #radio-option-group');
var i = $('#new-eval-question #radio-button-group input').size();
$('#new-eval-question #add-option-button').click(function(event) {
event.preventDefault();
i++;
$('<div class="form-group"><label class="control-label col-sm-2" for="radio_button_options[' + i + ']">Radio button option ' + i + '</label><div class="col-md-3"><input class="form-control" id="radio_button_options[' + i + ']" name="radio_button_options[' + i + ']" type="text" data-parsley-required="true"></div><a href="#" class="col-sm-1 remove-radio-input" id="remove-radio-input[' + i +']">Remove</a></div>').appendTo(rbDiv);
return false;
});
$('#new-eval-question').on('click', 'a.remove-radio-input', function(e) {
e.preventDefault();
var firstIndex = parseInt($(this).attr('id').
match(/\[(.*)\]/)[1], 10) + 1;
$(this).closest('div').remove();
i--;
var lastIndex = parseInt(
$('#new-eval-question #radio-button-group input').last().
attr('id').match(/\[(.*)\]/)[1], 10);
for (var j = firstIndex; j <= lastIndex; j++) {
var thisFormGroup =
$('#new-eval-question div:contains("Radio button option '
+ j + '")').closest('.form-group');
var replacement = (j-1);
thisFormGroup.find('label').
prop('for', 'radio_button_options[' + replacement + ']');
thisFormGroup.find('input').
prop('id', 'radio_button_options[' + replacement + ']');
thisFormGroup.find('input').
prop('name', 'radio_button_options[' + replacement + ']');
thisFormGroup.find('a').
prop('id', 'remove-radio-input[' + replacement + ']');
thisFormGroup.find('label').
text("Radio button option " + replacement);
};
return false;
});
/****************************************************************/
// Client-side validation
// Use Parsley to validate edit evaluation template forms.
window.ParsleyValidator.addMessage('en', 'required',
'This field is required.');
$('#new-eval-question form').parsley({ excluded: ':hidden' });
$('#edit-q-modal form').parsley({ excluded: ':hidden' });
/****************************************************************/
// var ordering_group = $('.ordering-select');
// var position_changes = {};
// ordering_group.each(function(event) {
// var dropdown = $(this).find("select");
// var question_num = $(dropdown).attr("id").match(/\[(.*)\]/)[1]
// var selected_position = $("option:selected", dropdown).text();
// position_changes[question_num] = selected_position;
// });
// console.log(position_changes);
// ordering_group.change(function(event) {
// var dropdown = $(this).find("select");
// var new_position = $("option:selected", dropdown).text();
// var old_position = $(dropdown).attr("id").match(/\[(.*)\]/)[1];
// // old_position corresponds to question_num above.
// $("select#\\_ordering\\[" + new_position + "\\]").
// val(position_changes[old_position]);
// position_changes[old_position] = new_position;
// });
});
$(文档).ready(函数(事件){
功能检查收音机(事件){
if($(“#新评估问题##问题类型”).val()=“单选按钮”){
$(“#新评估问题#单选按钮组”).show();
}否则{
$(“#新评估问题#单选按钮组”).hide();
};
};
检查收音机();
$(“#新评估问题##问题类型”)。在('change',checkRadio')上;
var rbDiv=$(“#新评估问题#单选选项组”);
var i=$(“#新评估问题#单选按钮组输入”).size();
$(“#新建评估问题#添加选项按钮”)。单击(函数(事件){
event.preventDefault();
i++;
$(“单选按钮选项”+i+“”)。附录(rbDiv);
返回false;
});
$(“#新评估问题”)。在('click','a.remove-radio-input',函数(e)上{
e、 预防默认值();
var firstIndex=parseInt($(this).attr('id')。
匹配(/\[(.*)\]/)[1],10)+1;
$(this).closest('div').remove();
我--;
var lastIndex=parseInt(
$(“#新评估问题#单选按钮组输入”).last()。
attr('id')。匹配(/\[(.*)\]/)[1],10);
对于(var j=firstIndex;jTurboLink已知会导致jQuery出现此类问题。请尝试将其关闭。这在某些情况下也可能有所帮助:对于记录,两个选项(删除TurboLink和安装jQuery.Turbolinks gem)都有效。可以按照以下说明删除TurboLink。
$(document).ready(function(event){
function checkRadio(event) {
if ($('#new-eval-question #_question_type').val() == 'Radio button') {
$('#new-eval-question #radio-button-group').show();
} else {
$('#new-eval-question #radio-button-group').hide();
};
};
checkRadio();
$('#new-eval-question #_question_type').on('change', checkRadio);
var rbDiv = $('#new-eval-question #radio-option-group');
var i = $('#new-eval-question #radio-button-group input').size();
$('#new-eval-question #add-option-button').click(function(event) {
event.preventDefault();
i++;
$('<div class="form-group"><label class="control-label col-sm-2" for="radio_button_options[' + i + ']">Radio button option ' + i + '</label><div class="col-md-3"><input class="form-control" id="radio_button_options[' + i + ']" name="radio_button_options[' + i + ']" type="text" data-parsley-required="true"></div><a href="#" class="col-sm-1 remove-radio-input" id="remove-radio-input[' + i +']">Remove</a></div>').appendTo(rbDiv);
return false;
});
$('#new-eval-question').on('click', 'a.remove-radio-input', function(e) {
e.preventDefault();
var firstIndex = parseInt($(this).attr('id').
match(/\[(.*)\]/)[1], 10) + 1;
$(this).closest('div').remove();
i--;
var lastIndex = parseInt(
$('#new-eval-question #radio-button-group input').last().
attr('id').match(/\[(.*)\]/)[1], 10);
for (var j = firstIndex; j <= lastIndex; j++) {
var thisFormGroup =
$('#new-eval-question div:contains("Radio button option '
+ j + '")').closest('.form-group');
var replacement = (j-1);
thisFormGroup.find('label').
prop('for', 'radio_button_options[' + replacement + ']');
thisFormGroup.find('input').
prop('id', 'radio_button_options[' + replacement + ']');
thisFormGroup.find('input').
prop('name', 'radio_button_options[' + replacement + ']');
thisFormGroup.find('a').
prop('id', 'remove-radio-input[' + replacement + ']');
thisFormGroup.find('label').
text("Radio button option " + replacement);
};
return false;
});
/****************************************************************/
// Client-side validation
// Use Parsley to validate edit evaluation template forms.
window.ParsleyValidator.addMessage('en', 'required',
'This field is required.');
$('#new-eval-question form').parsley({ excluded: ':hidden' });
$('#edit-q-modal form').parsley({ excluded: ':hidden' });
/****************************************************************/
// var ordering_group = $('.ordering-select');
// var position_changes = {};
// ordering_group.each(function(event) {
// var dropdown = $(this).find("select");
// var question_num = $(dropdown).attr("id").match(/\[(.*)\]/)[1]
// var selected_position = $("option:selected", dropdown).text();
// position_changes[question_num] = selected_position;
// });
// console.log(position_changes);
// ordering_group.change(function(event) {
// var dropdown = $(this).find("select");
// var new_position = $("option:selected", dropdown).text();
// var old_position = $(dropdown).attr("id").match(/\[(.*)\]/)[1];
// // old_position corresponds to question_num above.
// $("select#\\_ordering\\[" + new_position + "\\]").
// val(position_changes[old_position]);
// position_changes[old_position] = new_position;
// });
});