I have a jquery post code that can have two different reactions based on the response. if the response is a then i would like to alert() if it is not a then i would like to update a div (ratebox). However, one of the lines (below) isn't working. It appends the letter a onto the ratebox when it should bring up the alert. I checked that answerrating.php does produce the letter a.
JQUERY:
$.post('../answerrating.php' , {answer_id: answer_id , direction: 2} , function(response){
if (response == 'a'){ //this line doenst work
alert("error");
}
else {
ratebox.text(response);
}
});
Perhaps you get some whitespaces in the response, try using jQuery.trim():
if ($.trim(response) == 'a'){ //this line doenst work
alert("error");
}
You could also use firebug to check so that your response don't include any html.
If you are getting something other than plain text from the server try setting the dataType in your call:
$.post('../answerrating.php',
{
answer_id: answer_id,
direction: 2
},
function (response) {
//this line doenst work
if (response == 'a') {
alert("error");
} else {
ratebox.text(response);
}
},
dataType: 'text'
);
Related
My current code checks the country code and then changes the drop down value before submitting it for the things to refresh in right currency pricing, but I've tried things like, putting return; to stop it after running it once and moving return; on different line with no luck. I even combined the .val line with .change() thinking maybe having it on separate lines was running it multiple times and refreshing it multiple times.
Here is my code;
$.ajax({
url: "https://ipinfo.io/json",
type: "GET",
success: function(location) {
if (location.country === 'AU') {
$(".shopify-currency-form select").val("AUD").change();
} else if (location.country === 'AR') {
$(".shopify-currency-form select").val("ARS");
} else if (location.country === 'CA') {
$(".shopify-currency-form select").val("CAD");
} else if (location.country === 'US') {
$(".shopify-currency-form select").val("USD").change();
}
return;
}
} );
$('.shopify-currency-form select').on('change', function() {
$(this)
.parents('form')
.submit();
return;
});
I have a search box which is using the jqueryUI .autocomplete to get data via AJAX to give suggestions. The problem is that when a user hits enter before the autocomplete AJAX call to the source is done an error pops up in an alert box. "AJAX Error 0"
I assume when the user hits "Enter" when in the search box the browser is automatically stopping the AJAX call which resulting in a bad AJAX response which is triggering jqueryui to pop an error. Is there anyway to make this not happen? Here is some of the sample code of the autocomplete:
$("#searchBar_searchAll").on("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active ) {
event.preventDefault();
}
}).autocomplete({
source: function(request, response) {
$.getJSON('<?php echo CController::createUrl("inventory/searchAutocomplete"); ?>', {
searchString: extractLast(request.term),
searchScopeRecord: $("#searchScope_recordScope option:selected").val(),
searchScopeActive: $("#searchScope_activeScope option:selected").val(),
searchOptions: $("#searchBar_searchoption_searchAll option:selected").val()
}, response);
},
search: function() {
var searchString = extractLast(this.value);
if (searchString.length < 3)
return false;
},
focus: function() {
return false;
},
select: function(event, ui) {
window.location.href = '<?php echo CController::createUrl("inventory/update"); ?>' + '&id=' + ui.item.value;
return false;
}
});
Edit: The problem only happens sometimes upon further testing and it seems to happen almost every time on a slower machine. It seems related to how fast the machine executes the Javascript.
It seems that the AJAX error 0 occurs because when the user submits the search it cancels the AJAX request. It seems like when this is cancelled jquery-ui's .autocomplete is likely triggering an error pop-up due to getting no data. Is there anyway I could cleanly cancel the AJAX request then when someone hits enter or the submit button so that such an AJAX error does not occur?
Hi I think you are looking for enter search functionality.
you just need this property
$("#searchBar_searchAll").autocomplete({ autoFocus: true });
I didn't have your php api code so i put a sample array source for my autocomplete.
here is working example
another example with ajax source. but its already mentioned atleast we need to wait while ajax request should complete and return with results.
here is another example with ajax source.
The browser submits the form by default if you hit enter. While the ajax-request needs time (if not delivered from browser-cache), the browser-form-submit fires immediately and breaks your running ajax-request. As result you see those ajax error.
Maybe you can set a helper-variable if you press Enter and skip the JSON-function, if this var is true.
var bEnterPressed = false;
$("#searchBar_searchAll").on("keydown", function(event) {
var iKey = event.keyCode || event.which; // event.which is for old IE compatibility
bEnterPressed = false;
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active ) {
event.preventDefault();
}
// Detect Enter = key 37
if( iKey === 37 ) {
bEnterPressed = true;
}
}).autocomplete({
source: function(request, response) {
if( bEnterPressed ) { return false; } // skip ajax-request, if Enter was pressed
try {
$.getJSON('<?php echo CController::createUrl("inventory/searchAutocomplete"); ?>', {
searchString: extractLast(request.term),
searchScopeRecord: $("#searchScope_recordScope option:selected").val(),
searchScopeActive: $("#searchScope_activeScope option:selected").val(),
searchOptions: $("#searchBar_searchoption_searchAll option:selected").val()
}, response);
} catch(e) {
// ignore ajax-errors
}
},
// ...
});
I am trying to make the browser to remeber the ajax updated html. So when user hit the back button, it will go back to the ajax update html. I had found the following code from stackoverflow, but I had no idea how to use it. I had tried to replace the #Mytable with my class .post-content. But it doesn't work, do I miss something. Help, appreciate.
$('#pagination').click(function(e){
var data = {
action: 'post_pagination_ajax',
AjaxFrontNonce : Child_ajax.ajaxLoadUserNonce,
};
$.ajax({
url: ajax_front_gallery.ajaxurl,
type:'POST',
data: data,
success: function(data){
$('.post-content').fadeOut().html(data).fadeIn();
$fa_spen.fadeOut();
}
});
return false;
});
// the following code found on stackoverflow, but I don't know how to replace it with my code.
if (('localStorage' in window) && window['localStorage'] !== null) {
if ('myTable' in localStorage && window.location.hash) {
$("#myTable").html(localStorage.getItem('myTable'));
}
}/*remember the previous div position*/
// Replace the search result table on load.
if (('localStorage' in window) && window['localStorage'] !== null) {
if ('myTable' in localStorage && window.location.hash) {
$("#myTable").html(localStorage.getItem('myTable'));
}
}
I am trying a implement a simple if statement based on a cookie setting. The function is as follows:
function setHomePage() {
if ($.cookie('settingOne') == 'jjj')
{
$('.secO').css('display', 'block');
$('.secT').css('display', 'none');
}
else if($.cookie('settingOne') == 'ppp')
{
$('.secO').css('display', 'none');
$('.secT').css('display', 'block');
}
}
The function is called on load but not sure if the if statement syntax is correct.
If I alert the cookie setting I can confirm that they are set.
I can view the cookies setting in options->privacy.... etc and cookies are enabled.
If I remove the show and hide code and just add an alert it is not actioned making me believe the issue is with the if statement part.
Has anyone done something similar and confirm if my syntax is correct?
Can you try this?
function setHomePage() {
if ($.cookie('settingOne') == 'jjj')
{
$('.secO').show();
$('.secT').hide();
}
else if ($.cookie('settingOne') == 'ppp')
{
$('.secO').hide();
$('.secT').show();
}
}
Also check if your browser has Cookies enabled.
Please give the output for the following:
console.log ($.cookie('settingOne') == 'jjj');
console.log ($.cookie('settingOne') == 'ppp');
In my phonegap application i use jquery mobile. When retrieve the value from my DB the other fields are filled correctly(textbox values are filled correctly) works except checkbox. My problem is when the DB returns true or false whatever it is there is no changes in my checkbox. I struggled this issue for two days anyone suggest the solution to solve this one..
My coding is like:
if (results.rows.item(0).checkbox1 == true)
$("#checkbox1").attr('checked', 'checked');
else $("#checkbox1").removeAttr('checked');
Tried this also:
//$('#checkbox1').prop('checked', true).checkboxradio('refresh');
//$("input[id='checkbox1']").attr("checked",results.rows.item(0).checkbox1).checkboxradio("refresh");
solved:
like
if (results.rows.item(0).checkbox1 === 'true')
$("#checkbox1").attr('checked', 'checked');
else $("#checkbox1").removeAttr('checked');
Try this:
var dbValue = (results.rows.item(0).checkbox1 === true);
$('#checkbox1').prop('checked', dbValue);
Note the === in the first line. If your DB return value is something like 1 or the string "true", check for it explicitly.
You have to do a refresh once you set the values dynamically to the form elements in jQuery Mobile.
For your case the method it is :
document.addEventListener('deviceready', function() {
var a= true;
alert("hi");
if(a == true) {
$("#checkbox1").attr("checked",true);
$("#checkbox2").attr("checked",true);
} else {
$("#checkbox1").removeAttr('checked');
$("#checkbox2").removeAttr('checked');
}
$("#checkbox1").attr("checked",true).checkboxradio("refresh");
}, false);