2014-02-10 26 views
0
通过

我有以下的AJAX代码提交名称/电子邮件/信息参数,以“messageaction.cfm”模板,并显示原提交页面上的那些相同的3个参数(正常工作):提交按钮值不与阿贾克斯

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     function submitForm() { 
      $.ajax({type:'POST', url:'messageaction.cfm', data:$('#ContactForm').serialize(), success: function(response) { 
       $('#ContactForm').find('.form_result').html(response); 
      }}); 

      return false; 
     } 
    </script> 

      <form id="ContactForm" onsubmit="return submitForm();"> 
       Name: <input type="text" name="name" value=""><br> 
       Email: <input type="text" name="email" value=""><br> 
       Message:<br> <textarea style="width: 200px; height: 100px;" name="message"></textarea> 
       <br> 
       <input type="submit" name="Choice" id="Choice" value="One"> 
       <input type="submit" name="Choice" id="Choice" value="Two"> 
       <div class="form_result"></div> 
      </form> 

但是,我有2个提交按钮(“One”和“Two”的相应值),并希望能够检测到哪个按钮被按下。在一个正常的提交表单中(没有ajax),根据我点击哪个按钮,变量“Choice”与相应的“One”或“Two”正确显示。但在ajax表单中,无论我按哪个按钮,“选择”变量只显示相同的“0”(默认值)。

我已经尝试了2个其他ajax表单变体,但似乎无法传递输入提交按钮值的值。我做错了一定有一些非常基本的东西,但我已经尝试过所有我能想到的东西。任何建议将不胜感激!

回答

1

由于id是独一无二的,name属性应在同一form是唯一的,以及,你应该改变:

<input type="submit" name="Choice" id="Choice" value="One"> 
<input type="submit" name="Choice" id="Choice" value="Two"> 

到:

<input type="submit" name="ChoiceOne" id="ChoiceOne" value="One"> 
<input type="submit" name="ChoiceTwo" id="ChoiceTwo" value="Two"> 

,并与您的AJAX代码再试一次。确保你这次的目标是正确的:)

+0

菲利克斯,'欣赏小费,但我尝试过,结果仍然相同。我还删除了第二个输入按钮,以便有一个按钮“ChoiceOne”,但是当我点击它时,它仍然没有传回值“One”,而是仍然只显示默认的“0”。这真让我感到困惑。在标准表单提交中,显示值“ChoiceOne = One”,但是通过Ajax检索时,无论我尝试什么,它都只显示“ChoiceOne = 0”。 – user3293560

+0

我也尝试以下Ajax代码: \t <脚本类型= “文本/ JavaScript的”> \t \t $(函数(){ \t \t \t $( '#FF')形式({ \t \t \t。 \t成功:功能(数据){ 的document.getElementById( '数据')的innerHTML =数据; \t \t \t \t。} \t \t \t}); \t \t}); \t \t <表格ID = “FF” 行动= “messageaction.cfm” 方法= “POST”> user3293560

+0

使用document.getElementById('data')。innerHTML的同样的问题。它不会回传提交按钮本身的实际值“One”。 – user3293560

0

submit事件发生时,jQuery.serialize()不知道哪个按钮被点击了,所以在生成表单数据时很可能会跳过这些按钮。

您还必须处理每个按钮的点击事件,并手动传递按钮值。

另一种方法是在用户单击按钮时设置隐藏表单字段值,因为按钮点击事件将在表单提交之前得到处理。

+0

感谢您的提示! – user3293560