【问题标题】:using JQuery and Ajax to post a form使用 JQuery 和 Ajax 发布表单
【发布时间】:2010-06-08 13:09:12
【问题描述】:

我想使用 ajax 发布我的表单

我希望单选按钮执行此操作(没有提交按钮)

所以单击单选按钮将导致表单被提交或发布

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

我的代码正确吗?因为它不起作用,我不知道为什么?

<div id="Q1"> 
<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!"/> blue
<br />
<input type="radio" id="option1" name="answer[1]" value="false!"/> red
<br />
<input type="radio" id="option1" name="answer[1]" value="false!"/> green
<br />
<input type="radio" id="option1" name="answer[1]" value="false!"/> white
<br />
<br /> </Form>
<!-- <p id="greatp" style="display: none;"> GREAT !!<br /><br /><br /><br /><br /><br /><br /></p> -->
<img border="0" src="images/welldone3.png" width="230" height="182" id="greatp" style="display: none;"/>
<!-- <p id="sorryp" style="display: none;"> Sorry  !!<br /><br /><br /><br /><br /><br /><br /></p> -->
<img border="0" src="images/failed3.png" width="230" height="182" id="sorryp" style="display: none;"/>
<img src="images/false2.png"id="myImage1"style="display: none;"/>
<img src="images/correct2.png"id="myImage2"style="display: none;"/>
<!-- <input type= "submit" onclick="Next1()" value="Next"/> -->
<input type="image" src="images/next.png" name="image" width="80" height="30" onclick="Next1()">
</div>

jQuery 代码:

<script>
$(document).ready(function(){   
    $("#option1").click(function() {   

    var answer= $("#option1").value;

    var fff= $("#option1").name;

        $.ajax({   
            type: "POST",   
            url: "page2.php",   
            data: fff,  
            success: function(){   
                if(answer=='correct!')
                   {
                    $('#greatp').show();   
                    $('#myImage2').show();
                     $('#Question1').hide();

                    }
                 else
                    {
                      $('#sorryp').show();   
                      $('#myImage1').show();
                       $('#Question1').hide();
                    }


            }   
        });   
    return false;   
    });   
});  


</script>

【问题讨论】:

    标签: jquery html ajax


    【解决方案1】:

    首先,标签上的 ID 只能存在于一个标签中,它是唯一的标识符。那将是你的第一个问题 jQuery 将使用$("#someId") 抓取它找到的第一个元素。

    将其更改为一个类,这应该会有所帮助。 IE。 $(".someClass") 或根本不使用类并执行$("input:radio").click... 之类的操作

    对于数据,您已经很接近了,但我认为这不太可行,因为您需要将数据作为键/值对发送。您可以使用查询字符串或直接使用 json 对象来实现此目的。比如:

    var theData = {
     name: $(this).attr("name"),
     value: $(this).val()
    };
    
    $.ajax({
      data: theData
      ...
    });
    

    这是您正在寻找的thisthis 设置为单击的任何单选按钮,现在您再次硬编码到 ID 为 #option1 的第一个单选按钮,这是错误的。

    另外,考虑将$.get$.post 函数视为比$.ajax 稍微简单的函数。它们也更简洁,因此实现相同目标的代码更少。

    【讨论】:

    • 谢谢你,布拉德,数据呢?如何给 page2.php 它需要的东西?在这种情况下如何给它单选按钮的名称?我做的对吗?
    猜你喜欢
    • 2011-05-31
    • 2011-11-12
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多