Duplication possible:
Vérification du bouton radio spécifique est cochée
J’ai ces 2 boutons radio en ce moment pour que l’utilisateur puisse décider s’il a besoin ou non de frais de port:
Yes No
Je dois utiliser Jquery pour vérifier si le bouton radio «oui» est coché, et si c’est le cas, effectuez une fonction d’ajout. Quelqu’un pourrait-il me dire comment je ferais ça s’il vous plaît?
Merci pour toute aide
modifier:
J’ai mis à jour mon code pour cela, mais cela ne fonctionne pas. Est-ce que je fais quelque chose de mal?
//
$('input:radio[name="postage"]').change( function(){ if ($(this).is(':checked') && $(this).val() == 'Yes') { // append goes here } });
Ou, ci-dessus – encore une fois – en utilisant un peu moins superflu jQuery:
$('input:radio[name="postage"]').change( function(){ if (this.checked && this.value == 'Yes') { // note that, as per comments, the 'changed' // will *always* be checked, as the change // event only fires on checking an , not // on un-checking it. // append goes here } });
JQuery révisé (amélioré):
// defines a div element with the text "You're appendin'!" // assigns that div to the variable 'appended' var appended = $('').text("You're appendin'!"); // assigns the 'id' of "appended" to the 'appended' element appended.id = 'appended'; // 1. selects '' elements with the 'name' atsortingbute of 'postage' // 2. assigns the onChange/onchange event handler $('input:radio[name="postage"]').change( function(){ // checks that the clicked radio button is the one of value 'Yes' // the value of the element is the one that's checked (as noted by @shef in comments) if ($(this).val() == 'Yes') { // appends the 'appended' element to the 'body' tag $(appended).appendTo('body'); } else { // if it's the 'No' button removes the 'appended' element. $(appended).remove(); } });
var appended = $('').text("You're appendin'!"); appended.id = 'appended'; $('input:radio[name="postage"]').change(function() { if ($(this).val() == 'Yes') { $(appended).appendTo('body'); } else { $(appended).remove(); } });
Yes No
Essaye ça
if($("input:radio[name=postage]").is(":checked")){ //Code to append goes here }
Quelque chose comme ça:
if($('#postageyes').is(':checked')) { // do stuff }
$('input:radio[name="postage"]').change(function(){ if($(this).val() === 'Yes'){ // append stuff } });
Cela va écouter un événement de changement sur les boutons radio. Au moment où l’utilisateur clique sur Yes
, l’événement se déclenche et vous pouvez append tout ce que vous voulez au DOM.
if($('#test2').is(':checked')) { $(this).append('stuff'); }
$("input").bind('click', function(e){ if ($(this).val() == 'Yes') { $("body").append('whatever'); } });
Essaye ça:
if ( jQuery('#postageyes').is(':checked') ){ ... }
jQuery('input[name="inputName"]:checked').val()
Cela permettra d’écouter l’événement modifié. J’ai essayé les réponses des autres mais celles-ci n’ont pas fonctionné pour moi et finalement, celle-ci a fonctionné.
$('input:radio[name="postage"]').change(function(){ if($(this).is(":checked")){ alert("lksdahflk"); } });