0

我为网站制作了一个基于 ajax 的表单,它接受用户的输入并将其发送到数据库。所有字段都工作正常,但单选按钮在发送值时出现问题。我的ajax代码是

 <script type="text/javascript">
 function emp1() {
    .....................
xmlhttp.send("firstname=" + firstname.value + "&lastname=" + lastname.value  + "&company=" + company.value             
    + "&myemail=" + myemail.value
+ "&address=" + address.value
+ "&city=" + city.value
+ "&province=" + province.value
+ "&postalcode=" + postalcode.value
+ "&country=" + country.value
+ "&contact=" + contact.value
+ "&radio1=" + radio1.value
+ "&paymentmethod=" + paymentmethod.value
);

单选按钮的 html 表单代码是

<input type="radio" name="radio1" id="radio1" value="Free Shipping"  onclick="emp1();"> Free Shipping<br />  
<input type="radio" name="paymentmethod" id="paymentmethod"  value="abc"  onclick="emp1();">abc
<br />
<input type="radio" name="paymentmethod" id="paymentmethod" value="def"  onclick="emp1();" onchange="emp1();">def
<br /> 
 <input type="radio" name="paymentmethod" id="paymentmethod" value="ghi"  onclick="emp1();">ghi

任何帮助...... .. thnx提前

4

1 回答 1

0

您可以使用类似这样的方法在您的emp1()函数中使用 javascript 来获取选定的单选按钮 vlaue。它会给你选择的单选按钮值。

var inputs = document.getElementsByName("paymentmethod");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked) {
        var paymentmethod = inputs[i].value;
      }
    }

    alert(paymentmethod);
于 2013-02-06T04:59:26.037 回答