【问题标题】:Posting a form using AJAX使用 AJAX 发布表单
【发布时间】:2010-06-08 17:41:13
【问题描述】:

你知道如何发送我的单选按钮名称

myAjaxPostrequest.send(parameters);

参数可以这样:

var answername=document.getElementById('option1').name;

var parameters=answername;

?此代码用于使用 ajax 发布表单

我的 php 页面需要单击的单选按钮的名称

我尝试了这段代码,除了插入数据库之外,它可以按我的意愿工作。这意味着参数是问题

我要插入的是位于单选按钮名称的括号之间的数字。

我可以在没有 AJAX 的情况下发布表单,但现在我无法发送单选按钮的名称

关于我可以将什么作为函数myAjaxPostrequest.send(parameters); 的参数发送的任何线索?

<form id="Question1" method="post"> 
<br />
<P> The sky color is..
</P><img border="0" src="images/wonder.png" width="94" height="134"/><br /><br /><br />
<input type="radio" id="option1" name="answer[1]" value="correct!" onclick="submitFormWithAjax();"/> blue
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> red
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> green
<br />
<input type="radio" id="option1" name="answer[1]" value="false!" onclick="submitFormWithAjax();"/> white
</form>

【问题讨论】:

    标签: ajax forms post


    【解决方案1】:

    您可以通过以下方式将参数传递给ajax调用:

    var url = "get_data.php";
    var params = "lorem=ipsum&name=binny";
    http.open("POST", url, true);
    
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    
    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(params);
    

    【讨论】:

      【解决方案2】:

      你会使用:

      var checkedRadio = document.getElementsByName('nameOfRadios');
      var isChecked = checkedRadio.checked
      

      所以,要获取选中单选的值,请执行以下操作:

      var checkedRadio = document.getElementsByName('nameOfRadios');
      var isChecked = checkedRadio.checked;
      var checkedValue;
      for (var i=0; i<checkedRadio.length; i++) {
          if (checkedRadio[i].checked){
            checkedValue = checkedRadio[i].value;
         }
      }
      

      【讨论】:

      • 谢谢你 Dale,但我不想知道检查收音机的价值。我已经知道了,也让我的 php 知道了。但是如何使用 myAjaxPostrequest.send 将我的电台名称发送到 php 页面(我应该在这里放什么??);?
      • 你用什么 ajax 引擎发帖?您需要知道语法是什么才能为参数赋值。通常它类似于 myAjaxPostrequest.send(param1: 'radioName', param2: 'othervalue');对于参数语法。
      猜你喜欢
      • 1970-01-01
      • 2011-09-22
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多