How can I send radio button value in PHP using javascript?
本问题已经有最佳答案,请猛点这里访问。
我已经在这里呆了一个星期,读了所有的stackoverflow和google,我不能解决这个问题。每次我测试它,我都会得到第一个单选按钮,不管我点击哪个。
我收到的邮件是:
1 2 3 4 | Name: test Email: test@live.com phone: 9545027557 type: Residential |
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <form name="controls" id="controls" novalidate> <input type="text" class="form-control" placeholder="Full Name" id="name" required data-validation-required-message="Please enter your name" /> <p class="help-block"> </p> <input type="email" class="form-control" placeholder="Email" id="email" required data-validation-required-message="Please enter your email" /> <label class="btn btn-primary active"> <input type="radio" class="form-control" id="optionu" name="optionu" value="Residential" checked > Residential </label> <label class="btn btn-primary"> <input type="radio" class="form-control" id="optionu" name="optionu" value="Commercial"> Commercial </label> <label class="btn btn-primary"> <input type="radio" class="form-control" id="optionu" name="optionu" value="Handyman"> Handyman </label> <p> </p> <!-- For success/fail messages --> <button type="submit" class="btn btn-danger pull-right">Request</button><br /> </form> |
号
联系u me.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | var name = $("input#name").val(); var email = $("input#email").val(); var phone = $("input#phone").val(); var optionu = $("input#optionu").val(); var firstName = name; // For Success/Failure Message // Check for white space in name for Success/Fail message if (firstName.indexOf(' ') >= 0) { firstName = name.split(' ').slice(0, -1).join(' '); } $.ajax({ url:"bin/contact_me.php", type:"POST", data: {name: name, email: email, optionu: optionu, phone: phone}, cache: false, success: function() { // Success message $('#success').html(""); $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×") .append("</button>"); $('#success > .alert-success') .append("Your message has been sent."); $('#success > .alert-success') .append(''); |
联系u me.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $name = $_POST['name']; $optionu_value = $_POST['optionu']; $phone = $_POST['phone']; $email_address = $_POST['email']; // create email body and send it $to = '[email protected]'; // put your email $email_subject ="Contact form submitted by: $name"; $email_body ="You have received a new message. ". " Here are the details: Name: $name ". "Email: $email_address phone: $phone type: $optionu"; |
。
首先,你使用同一个ID 4次。ID在文档中必须是唯一的,不能将同一ID设置为4个元素。我也认为你从单选按钮中获取价值的方式是错误的。
这或多或少就是如何从单选按钮中获取值的方法:(另一个so问题的例子)
1 | var optionu = $('input[name=optionu]:checked', '#controls').val() |
同时从所有输入[radio]元素中删除id。
1 2 3 4 5 6 7 8 9 | <label class="btn btn-primary active"> <input type="radio" class="form-control" name="optionu" value="Residential" checked /> Residential </label> <label class="btn btn-primary"> <input type="radio" class="form-control" name="optionu" value="Commercial" /> Commercial </label> <label class="btn btn-primary"> <input type="radio" class="form-control" name="optionu" value="Handyman" /> Handyman </label> |
号
试试这个:
1 | $('input[name=optionu]:checked').val() |
工作小提琴:http://jsfiddle.net/robertrozas/9dyrr/
你必须找到收音机的检查值。你只需要得到第一个无线元素的值。
如何通过jquery知道选择了哪个单选按钮?
编辑:不能对每个单选按钮使用相同的ID。您必须给它一个类名,或者使用jquery按名称引用按钮。