我正在尝试使用 jquery 对话框做一个搜索表单。请参阅下面的完整程序。我的电脑上确实有一个服务器在运行,并且 http://localhost/search .几乎没有疑问
- ajax 调用出了点问题,因为我收到一条错误消息。我也尝试了 GET 请求。
- 在 chrome 中,服务器的请求类型是 POST ,但在 Iceweasel (Firefox) 中是 OPTIONS
- 在 Iceweasel (Firefox) 中,模态窗口保持打开状态,并显示错误。但在谷歌浏览器中,模态窗口关闭。这可能与 2
- 下面的 javascript 代码有什么问题。我不是 JS 专家。
非常感谢,
bsr.
<html><head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
</head>
<body>
<input id="myButton" name="myButton" value="Click Me" type="button" />
<div id="myDiv" style="display:none">
<form id="myform">
<input name="q" placeholder="Search ..">
</form>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<script type="text/javascript">
jQuery(document).ready( function(){
jQuery("#myButton").click( showDialog );
$myWindow = jQuery('#myDiv');
$myWindow.dialog({ width: 400, autoOpen:false, title:'Hello World',
overlay: { opacity: 0.5, background: 'black'},
buttons: {
"Submit Form": function() { $('form#myform').submit();},
"Cancel": function() {$(this).dialog("close");}
}
});
});
var showDialog = function() {
$myWindow.show();
$myWindow.dialog("open");
}
var closeDialog = function() {
$myWindow.dialog("close");
}
var successFn = function (response) {
$("#myform").parent().html('').html(response);
}
var errorFn = function (xhr, ajaxOptions, thrownError){
$("#myform").parent().html('').html(xhr.statusText);
}
$('form#myform').submit(function(){
$.ajax({
type: 'post',
dataType: 'html',
url: '/search',
async: false,
data: $("#myform").serialize(),
success: successFn,
error: errorFn
});
});
</script>
</body></html>
最佳答案
你可能需要这个。
// include modal into form
$(this).parent().appendTo($("form:first"));
编辑:
这样,您就可以将对话框添加到表单中,从而可以回发
您可以在打开事件中添加它。
尝试使用 json 作为数据类型,jSon是一个很棒的ajax工具=)
关于javascript - Jquery 对话框 : Form submission,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5680030/