Reputation: 1837
On a webshop, I need to check if the user has checked this payment choice, and if he checked the CGU rules.
So this my html :
<div id="choixPaiement">
<div class="choixDuReglement">
<ul>
<li><input type="radio" name="buttonModePaiement" value="tata"></li>
<li><input type="radio" name="buttonModePaiement" value="toto"></li>
<li><input type="radio" name="buttonModePaiement" value="titi"></li>
<li><input type="radio" name="buttonModePaiement" value="tutu"></li>
</ul>
</div>
<div id="accepte_cgv">
<p> <input type="checkbox" name="check_cgv" id="check_cgv"><label>Lu et approuvé</label></p>
<p><a onclick="validCommande();" class="btnValidCommande" href="javascript:;"><span>Valid my order</span></a></p>
</div>
</div>
And this is my javascript :
function validCommande(){
var paiement = $("input[@name=buttonModePaiement]:checked").val();
alert(paiement);
if(paiement == "undefined"){
alert('please choose payment method');
}
alert($('#check_cgv').is(':checked'));
}
when radiobutton and checkbox are not checked, I get two alert message : "undefined" and "false" (so that is correct).
But when I'm checking the checkbox, and no radiobutton, I get it : "on" and "true".
Why ?
Upvotes: 1
Views: 805
Reputation: 94
if u want to validate the radio button the do this way
<form name="as" method="post" action="n.php">
<input type="radio" id="x1" name="red">
<input type="radio" id="x2" name="red">
<input type="radio" id="x3" name="red">
<input type="radio" id="x4" name="red">
<input type="submit" value="button" onclick="return xyz()">
</form>
function xyz()
{
var x = document.getElementsByName("red");
for (var i=0; i<x.length; i++)
{
if (x[i].checked) {
return true;
}
}
// No radio button checked, return false.
alert("hello");
return false;
}
Upvotes: 0
Reputation: 14219
Here is what I would do to your code and why:
<div id="choixPaiement">
<div class="choixDuReglement">
<ul>
<li><input type="radio" class="buttonModePaiement" value="tata"></li>
<li><input type="radio" class="buttonModePaiement" value="toto"></li>
<li><input type="radio" class="buttonModePaiement" value="titi"></li>
<li><input type="radio" class="buttonModePaiement" value="tutu"></li>
</ul>
</div>
<div id="accepte_cgv">
<p> <input type="checkbox" name="check_cgv" id="check_cgv"><label>Lu et approuvé</label></p>
<p><a id="btnValidCommande" href="#"><span>Valid my order</span></a></p>
</div>
</div>
$(document).ready(function(){
$('#btnValidCommande').click(function(e){
e.preventDefault(); // stop the link from trying to navigate
validCommande();
});
});
function validCommande() {
var paiement = $(".buttonModePaiement:checked").val();
if (!paiement) {
alert('please choose payment method');
}
alert($('#check_cgv').is(':checked'));
}
Use classes when repeating identifiers, not names. Names are supposed to be unique for things like form submission. name="buttonModePaiement"
should be class="buttonModePaiement"
.
Append your events in JS code, not inline. Notice I removed the onclick="validCommande();"
from your link and append it in the DOM ready event.
You don't need to (nor should you) compare against "undefined". This will suffice (paiement == "undefined")
---> (!paiement)
Working fiddle: http://jsfiddle.net/hMdKT/3/
Upvotes: 0
Reputation: 83356
I think this is what you want
function validCommande() {
var paiement = $("input[name='buttonModePaiement']:checked");
if(paiement.length === 0)
alert('please choose payment method');
else
alert(paiement.val());
alert($('#check_cgv').is(':checked'));
}
Also, you're not closing your input tags:
<input type="radio" name="buttonModePaiement" value="tata" />
Upvotes: 1