I have ajax response containing two radio element.i want to check if radio element is checked in response.
I m using below code to check radio status but not working.
$('#input[type=radio]').each(function(){
alert($(this).attr('checked'));
});
as i know this is not working due to ajax response. but how to fire js code to check even in ajax resoponse.
any help would be much appreciate.
try this:
$(document).find('input[type="radio"]').each(function(){
if($(this).is(':checked')){
//your code
}
});
Try this
$('#input[type=radio]').each(function(){
if($(this).is(':checked')){
}
});
use prop
$('input[type=radio]').each(function(){
alert($(this).prop('checked'));
});
and #input only if your radio have id="input". if you want check all radio, could be only
input[type=radio]
as the example
Your issue is your selector which contains # for id:
$('#input[type=radio]')
//-^------------------------# is used to select an id not the tags
try this:
$('input[type="radio"]').each(function(){
alert(this.checked); // returns "true" if checked/ "false" if not checked
});
Related
I need to check if a checkbox next to a textbox is checked or not, but the result I'm getting from that condition in my code is always true, so that's wrong.
I've made some tests using the next() method from jQuery. I'm pointing to the next checkbox close to the textbox, which is working fine.
This code is always returning true, whether the checkbox is checked or not:
if ($(this).next('.isFileSelected:checkbox:checked')) {
//do some other stuff...
}
I tried these changes too, but I'm getting the same results:
if ($(this).next('input:checkbox:checked')) {
//do some other stuff...
}
How can I accomplish this using jQuery or pure JavaScript code?
You're looking for the prop() attribute checked:
if ($(this).next('.isFileSelected:checkbox').prop('checked')) {
// Checked
}
Or, alternatively, you can use .is() to check for the :checked pseudo-selector:
if ($(this).next('.isFileSelected:checkbox').is(":checked")) {
// Also Checked
}
Hope this helps! :)
This successfully returns a result, but the result is useless to me. In the php file, I have this simple code: var_dump($_POST['rememberMe']);
Whether the checkbox is marked or not, the result is string(2) "on". How do I get a result that changes based on whether the checkbox is marked?
Thanks.
Remember Me: <input type="checkbox"
id="remember">
<div id="result" style="margin-top:20px;">
<script>
$("#submit").click(function(){
submitLogin();
});
$("#password").keyup(function (f){
if (f.keyCode == 13) {
submitLogin();
}
});
function submitLogin(){
$.post("php/test.php", {
loginUsername:$('#username').val(),
loginPassword:$('#password').val(),
rememberMe:$('#remember').val()
}, function(data){
$("#result").text(data);
});
}
</script>
The value attribute is mandatory for checkboxes and defaults to on. This applies whether the checkbox is checked or not, the value is always the same. What you want is to check the state of the checkbox (not its value).
$('#remember').prop("checked")
Btw, this is way faster than .is(:checked), see http://jsperf.com/prop-vs-ischecked/5 for a comparison.
It's better if you serialized the form that you are sending via post $("form").serialize(); Then, in the target page, just check if $_POST['rememberMe'] exists. (If the checkbox isn't checked, isset for $_POST['rememberMe'] will be equal to false)
This:
rememberMe:$('#remember').val()
Needed to become:
rememberMe:$('#remember').is(':checked')
I have the following JS code:
else if ($('#tos').is(':not(:checked)')){
alert("Error");
}
And the tos element:
<input type="checkbox" name="tos" value"1">
When I click my submit button, without checking it, the error doesn't pop up. Any reason why?
Your selector $('#tos') looks for an element with id tos which your checkbox does not have. You can either add the id attribute to the input element or use the name attribute to find the element
else if (!$('input[name="tos-pp"]').is(':checked')){
Your jQuery selector is not returning anything because the checkbox doesn't have an id of tos. Also, the correct syntax for checking if a checkbox/other input is checked is as follows:
else if (!$('input:checkbox').is(':checked')){
alert("error");
}
I composed a fiddle for your reference. Please see the code, it may help you out.
not sure what is wrong with this jquery code: http://jsfiddle.net/aCABM/
I want to to grey out the dropdown menu when 'yes' is selected. When no is selected it can be selected if the user so wishes.
Select the elements by radio button name there is no need to select them individually by there ids and use prop instead of attr since you are manipulating its disabled property. Try this.
$(function(){
$("input[name='discount']").click(function() {
$("#discountselection").prop("disabled", this.value == 'Yes');
});
});
Demo
In your fiddle there were few issues.
Missed to include jQuery library
You cannot just begin your markup with td, it should be enclosed with table and tr element.
In the Js window you just have to put your js, no need to have script tags.
check updated code here
http://jsfiddle.net/ylokesh/aCABM/1/
Update code here:
http://jsfiddle.net/aCABM/6/enter link description here
You need to wrap your code in a $(document).ready() function, like so:
$(document).ready(function() {
$("#Yes").click(function() {
$("#discountselection").attr("disabled", false);
//$("#discountselection").show(); //To Show the dropdown
});
$("#No").click(function() {
$("#discountselection").attr("disabled", true);
//$("#discountselection").hide();//To hide the dropdown
});
})
Also, you didn't setup jsFiddle correctly, it required jQuery library and (document)ready()
You can use this :
$("#Yes").click(function() {
$("#discountselection").show();
});
$("#No").click(function() {
$("#discountselection").hide();
});
and remove the disable="disable".
Good-Luck !
I downloaded the script from emposha.com/javascript/fcbklistselection-like-facebook-friends-selector.html and made a few modifications, as the source wasn't working as required (When submitting the form, all of the values were being sent to the php handler, not just the checked ones).
My working example is in this Demo.
My issue is this: how can I check a group of <lis> so that I could have a 'Select All' button?
You can do it like this:
$("#fcbklist li input:checkbox").attr("checked", true);
Or if you want a toggle on that select all box that checks when it's checked, unchecks all when it's not, do this:
$("#selectAll").change(function() {
$("#fcbklist li input:checkbox").attr("checked", this.checked);
});
To get the effect you want with the plugin though, you'll need to fire a click on the parent as well, like this:
$("#selectAll").change(function() {
$("#fcbklist li input:checkbox").attr("checked",this.checked).parent().click();
});
You can try a working sample here.