Double Possible:
Vérifier spécifiques bouton radio est cochéJ'ai ces 2 boutons radio au moment de sorte que l'utilisateur peut décider si elles ont besoin frais de port compris dans le prix ou pas:
<input type="radio" id="postageyes" name="postage" value="Yes" /> Yes <input type="radio" id="postageno" name="postage" value="No" /> No
J'ai besoin d'utiliser Jquery pour vérifier si le " oui " bouton radio est coché, et si c'est un ajout de la fonction. Quelqu'un pourrait-il me dire comment je le ferais s'il vous plaît?
Merci pour toute aide
edit:
J'ai mis à jour mon code, mais ça ne fonctionne pas. Suis-je en train de faire quelque chose de mal?
<script type='text/javascript'> // <![CDATA[ jQuery(document).ready(function(){ $('input:radio[name="postage"]').change(function(){ if($(this).val() == 'Yes'){ alert("test"); } }); }); // ]]> </script>
Réponses
Trop de publicités?$('input:radio[name="postage"]').change(
function(){
if ($(this).is(':checked') && $(this).val() == 'Yes') {
// append goes here
}
});
Révisée (amélioration de l'-certains) de jQuery:
// defines a div element with the text "You're appendin'!"
// assigns that div to the variable 'appended'
var appended = $('<div />').text("You're appendin'!");
// assigns the 'id' of "appended" to the 'appended' element
appended.id = 'appended';
// 1. selects '<input type="radio" />' elements with the 'name' attribute 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();
}
});
Références:
ShankarSangoli
Points
45345
Mrchief
Points
25418
Shef
Points
21595
genesis
Points
32591