can anyone tell me why this line isnt working? (i'm trying to access the value property of a checkbox and write it into my variable or otherwise if its not checked an empty string)
var ri_ort = $('#ri_ort').attr('checked')?$('#ri_ort').val():'';
Thx a lot
尝试的is
:
var ri_ort = $('#ri_ort').is(':checked') ? $('#ri_ort').val() : '';
In jQuery, you can check attributes like checked
and disabled
using the prop
method:
$('#ri_ort').prop('checked'); // returns true or false
prop
also can be used to set the value:
$('#ri_ort').prop('checked', false); // removes the `checked` attribute
Here is an example: http://jsfiddle.net/BeKa4/1/
As of jQuery 1.6, attr
method doesn't return a boolean value, it returns undefined
, you should use prop
or is
method instead:
var $elem = $('#ri_ort');
var ri_ort = $elem.prop('checked') ? $elem.val(): '';
Or if you want to listen to the change event:
$(function() {
var ri_ort = '';
$('#ri_ort').change(function(){
ri_ort = this.checked ? this.value : '';
})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.