我有一个网站,有一个投票调查模块。一项民调控制器通过接受包含轮询ID和响应ID作为参数,以 http://example.com/poll 。
I have a website that has a voting poll module. A poll controller accepts a vote via a POST
request containing the poll ID and response ID as parameters to http://example.com/poll
.
本网站的站点范围模板采用了目前的投票中的侧边栏;它与操作简单的形式
属性设置为前面提到的网址。我,但是,使用jQuery劫持这个异步提交投票表决。
The site-wide template of this site features the current poll in the sidebar; its a simple form with the action
attribute set to the aforementioned URL. I am, however, highjacking this with jQuery to submit poll votes asynchronously.
这是功能我迄今:
$('form.poll').submit(function() {
var form = this;
var response = $('div.response', form);
if ($('input:checked', form).length == 0) {
response.fadeOut('fast', function() {
$(this).html('<p class="error">Please select an option.</p>').fadeIn('fast');
})
}
else {
var action = $(form).attr('action');
$.post(action, $(form).serialize(), function(data) {
alert('Data loaded: ' + data);
});
$('fieldset', form).fadeOut('fast');
response.fadeOut('fast', function() {
$(this).html('<p class="success">Vote successfully added.</p>').fadeIn('fast');
});
}
return false;
});
正如你所看到的,它只是截取被提交的表单,然后执行 POST
使用jQuery,而不是一个完整的页面请求,所以游客永远不会离开网页,他们再上。
As you can see, it merely intercepts a form being submitted and then executes the POST
using jQuery rather than a full page request, so the visitor never leaves the page they're on.
我的问题是:整个页面的HTML回来了 $交
响应(与行警报()调用)。我怎样才能挑选出的含量
#内容
&LT; DIV&GT;
返回的,到HTML的标签被用作在我的轮询形式的反应?该加价投票表决的形式是:
My problem is: the whole page's HTML comes back in the $.post
response (the line with the alert()
call). How can I pick out the content of the #content
<div>
tag of the HTML that is returned, to be used as a response in my poll form? The mark-up for the poll form is:
<form action="/poll" method="post" id="poll" class="poll">
<h3>Poll</h3>
<p class="question">Who do you think will win the Lockdown main event?</p>
<div class="response"><!--//--></div>
<fieldset>
<input type="hidden" name="poll" value="1" />
<ul class="options">
<li><label for="option_1"><input type="radio" name="response" value="1" id="option_1" /> Mr Anderson</label></li>
<li><label for="option_2"><input type="radio" name="response" value="2" id="option_2" /> Rob Van Dam</label></li>
<li><label for="option_3"><input type="radio" name="response" value="3" id="option_3" /> Sting</label></li>
</ul>
<input type="submit" name="vote" value="Vote" class="button" />
</fieldset>
</form>
和我想插入响应到恰当地命名为 .response
DIV
标记。任何帮助,指针或建议将是多少AP preciated。
And I want to insert the response into the aptly-named .response
div
tag. Any help, pointers, or suggestions would be much appreciated.
一个更好的解决办法来检测,这是一个AJAX请求的服务器端,只返回你所需要的内容,许多框架有这种内置的,但你可以手动实现通过检查 HTTP_X_REQUESTED_WITH
头大多数主要JS框架(包括jQuery的)执行AJAX请求时添加类似的东西。
A better solution would be to detect that it is an AJAX request server-side and only return the content you need, many frameworks have this built in but you could manually implement something similar by checking for the HTTP_X_REQUESTED_WITH
header added by most major JS frameworks (including jQuery) when performing AJAX requests.
在PHP中的垃圾例子是沿着线的东西:
A rubbish example in PHP would be something along the lines of:
<?php if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest' ) : ?>
<html>
<head/>
<body>
<?php endif ?>
<p>Martin's form stuff!</p>
<?php if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest' ) : ?>
</body>
</html>
<?php endif ?>