[英]Can't seem to figure out why my javascript isn't working in IE and Chrome
问题我有这个js,由于某种原因它不能在IE或Chrome中使用,但是它确实可以在FF中使用,帮助将非常感谢。
旁注,我需要将其用作更改功能-出于某些奇怪的原因,单击功能可在IE和Chrome中工作,但是在这种情况下,我需要将其更改为RubyRails应用程序。
这是我的代码..
$(document).ready(function () {
$('#emp_id').change(function () {
var url = "/user/populate_form?emp_id=" + $(this).val();
$.getJSON(url, function (data) {
if (!(data.emp_first_name === undefined))
$('#emp_first_name').val(data.emp_first_name);
if (!(data.emp_last_name === undefined))
$('#emp_last_name').val(data.emp_last_name);
if ((data.error * 1) == 404) {
alert("The employee ID entered was not found!");
} else {
window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
}
});
});
});
我也尝试过在IE和Chrome浏览器中仍然无法使用
$(document).ready(function () {
$('#emp_id').on('change', function () {
var url = "/user/populate_form?emp_id=" + $(this).val();
$.getJSON(url, function (data) {
if (!(data.emp_first_name === undefined))
$('#emp_first_name').val(data.emp_first_name);
if (!(data.emp_last_name === undefined))
$('#emp_last_name').val(data.emp_last_name);
if ((data.error * 1) == 404) {
alert("The employee ID entered was not found!");
} else {
window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
}
});
});
});
也尝试过这一点,但在IE和Chrome中仍然不起作用
$(document).ready(function () {
$('#emp_id').keyup('onchange', function () {
var url = "/user/populate_form?emp_id=" + $(this).val();
$.getJSON(url, function (data) {
if (!(data.emp_first_name === undefined))
$('#emp_first_name').val(data.emp_first_name);
if (!(data.emp_last_name === undefined))
$('#emp_last_name').val(data.emp_last_name);
if ((data.error * 1) == 404) {
alert("The employee ID entered was not found!");
} else {
window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
}
});
});
});
这是我的查看代码
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :emp_id, tabindex: 1, id: 'emp_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :emp_first_name, tabindex: 1, id: 'emp_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
<div class='row form-group'>
<div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
<%= f.text_field :emp_last_name, tabindex: 1, id: 'emp_last_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
</div>
</div>
jQuery Change事件仅在输入字段失去焦点时触发。
编辑:
$(document).ready(function(){
$('#emp_id').change(function () {
var url = "/user/populate_form?emp_id=" + $(this).val();
$.getJSON(url, function (data) {
data = JSON.parse(data);
if (data.emp_first_name !== undefined) {
$('#emp_first_name').val(data.emp_first_name);
}
if (data.emp_last_name !== undefined) {
$('#emp_last_name').val(data.emp_last_name);
}
if (parseInt(data.error) === 404) {
alert("The employee ID entered was not found!");
} else {
window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
}
});
});
编辑2:我认为示例代码中存在语法错误...
这应该为您工作:
$(document).ready(function() {
$('#emp_id').keyup(function() {
var url = "/user/populate_form?emp_id=" + $(this).val();
$.getJSON(url, function (data) {
if (typeof(data.emp_first_name) !== 'undefined')
$('#emp_first_name').val(data.emp_first_name);
if (typedof(data.emp_last_name) !== 'undefined')
$('#emp_last_name').val(data.emp_last_name);
if (parseInt(data.error) == 404) {
alert("The employee ID entered was not found!");
}
else {
window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
}
});
});
});
好吧,我知道了...我要做的就是将$('#emp_id')。change(function(){更改为$('#emp_id')。blur(function(){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.