2013-02-12 101 views
3

我被这个问题困住了,我真的不知道为什么它不起作用。引导弹出窗口内的Ajax

如果我在引导程序的弹出窗口中使用此代码,它可以工作,但只要我在popover内使用它,它就不再工作。我的意思是,如果表单是从弹出窗口提交的,它就像一个正常窗体一样,收取新的页面而不是像AJAX脚本那样操作。

  $("#share-popover").submit(function(){ 
       //get the url for the form 
       var url=$("#form_search").attr("action"); 
       alert($("#search-value").val()); 

       //start send the post request 
       $.post(url,{ 
        formName:$("#search-value").val(), 
        other:"attributes" 
       },function(data){ 
        if(data.responseCode==200){   
         $('#output').html(data.greeting); 
         $('#output').css("color","red"); 
        } 
        else if(data.responseCode==400){ //bad request 
         $('#output').html(data.greeting); 
         $('#output').css("color","red"); 
        } 
        else{ 
         alert("An unexpeded error occured."); 
        } 
       }); 
       return false; 
      }); 

按钮:

<button id="share-dropdown" style="margin-right:5px" class="btn btn-warning pull-right" type="button" rel="popover" data-placement="bottom" data-html="true"><i class="icon icon-plus icon-white"></i> Share</button> 

JS:

 $('#share-dropdown').popover({ 
      html : true, 
      content: function() { 
       return $("#share-popover").html(); 
      } 
     }); 

的HTML内容:

<div id="share-popover" style="display: none"> 
    <form id="form_search" action="{{ path('weezbook_site_ajax_search') }}" method="POST"> 
    <input id="search-value" type="text" value="Book title, authors, isbn" class="share-input" name="search-value" /> 
    <input type="submit" /> 
    </form> 

我用这与Symfony2的和我的控制器返回JSON。

我真的不明白为什么它的工作盒子的外面,而不是内部...

回答

0

编辑:

我没有尝试,但$(document).on('submit', '#share-popover', function() {});更换.submit()应该工作,由于DOM被渲染后的内容加载,我不能”这时使用.submit()。


我不知道为什么,但用另一个替换.submit()方法解决了这个问题。

这里是我做了改变:

<input id="search-value" type="text" placeholder="Book title, authors, isbn" class="share-input" name="search-value" onkeypress="return runScript(event)" /> 


function runScript(e) { 
    if (e.keyCode == 13) { 
    ... 
    return false; 
    } 
} 
0

你与json工作,你有你的post指定它这样

$.post(url,{ data: something },function(data){some process}, 

'json');// define the type as json 
+1

没有解决我的问题,但对于试图感谢:) – BaNz 2013-02-15 09:50:41