I have a form which submits when any change is made on any of the radio input
's.
Inside the form I have two different radio groups. I am trying to output a message on my #message
on pjax:start
when any of the radios from group2
only is triggered.
<form id="myForm">
<input type="radio" name="group1" val="ga_1_1">
<input type="radio" name="group1" val="ga_1_2">
<input type="radio" name="group2" val="ga_2_1">
<input type="radio" name="group2" val="ga_2_2">
</form>
<span id="message"></span>
Here is my simplyfied approach:
$('input[name="group2"]').on('change', function(){
$(document).on('pjax:start', function() {
$('#message').text( 'Loading...' );
});
$(document).on('ready pjax:success', function() {
$('#message').text( 'Loaded' );
});
});
Problem is that the message triggers even if I click on radio group1
.
When the Submit Button is clicked, first the record is inserted into the database and then using ClientScript RegisterStartupScript function, the success message is displayed in JavaScript alert message box. script – The JavaScript code that will display the Alert message box.
So, let’s get started with HTML coding to display a message box on a button click. First of all, create a button element that will be used to reveal the message box. You can also create a div element virtual button to display a message box in HTML. After that, create HTML for message box and background dim overlay as follows:
//Display success message. string message = "Your details have been saved successfully."; 'Insert record here. 'Display success message. Dim message As String = "Your details have been saved successfully."
Welcome to a quick tutorial on how to display messages in Javascript. Just started with Javascript and wondering how to display a message? The common ways to show messages in HTML and Javascript are: Alert, prompt, confirm. Output the message to the developer’s console. Dialog box (popup box).
You've the right selector no need for change it, you could use $.pjax
callbacks directly :
$('input[name="group2"]').on('change', function(){
$.pjax({
url: '/url',
container: '#container',
beforeSend: function(){
$('#message').text( 'Loading...' );
},
complete: function(){
$('#message').text( 'Loaded' );
}
});
});
Or also you could put out the callbacks and use send & complete
instead as are the best if you're in loading purpose as described in the Official Documentation :
pjax:send & pjax:complete are a good pair of events to use if you are implementing a loading indicator
$(function(){
$('input[name="group2"]').on('change', function(){
$.pjax({url: "/url", container: '#container'})
});
$(document).on('pjax:send', function() {
$('#message').text( 'Loading...' );
})
$(document).on('pjax:complete', function() {
$('#message').text( 'Loaded' );
})
})
NOTE : No need for the ready
callback since it will init your #message
with Loaded
when the page is loaded before any $.pjax
request.
Take a look to best way to implement an overlay with pjax.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With