可能重复:
暂停表单提交以进行验证
这是对此的延续帖子.所以基本上我在iframe中上传文件.但在提交之前,我从表单中获取数据并使用django的内置系统检查它们的有效性(目前它只是一个虚函数,需要foo:bar,并返回json result ="true").我使用两个按钮 - 假可见,调用验证和第二 - 隐藏,提交表单.使用下面的代码,我能够执行验证,然后当它的结果是肯定的形式提交.现在,如果我对表单进行硬编码,我的警报就不会显示,而且我很确定没有执行上传(不幸的是,上传列表每8小时刷新一次,所以我会知道它是否在一段时间内有效).如果未指定目标,则使用重定向上载文件,因此省略整个"submit"事件侦听器.更重要的是,整个代码没有 在Chrome中完全可以工作.在接收验证响应和显示我以前从未见过的响应之间需要2-3s(看看firebug).我真的很想让它发挥作用,因为我已经浪费了2天而没有任何结果.
示例链接:
http://ntt.vipserv.org/artifact/
JS:
<script>
$('#fake_upload_submit').click(function(e){
e.preventDefault();
var fileUploadForm = document.getElementById('file_upload_form');
fileUploadForm.addEventListener("submit", function() {
alert("sent in iframe");
fileUploadForm.target = 'upload_target';
}, false);
$.ajax({
type: "POST",
url: '/artifact/upload/check-form/',
data: 'foo=bar',
dataType: "json",
success: function(data){
if(data['result'] == "true"){
$("#message").show();
$("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
setTimeout(function(){
$("#message").fadeOut("slow", function () {
$("#message").hide();
});
}, 1500);
fileUploadForm.submit();
}
else{
$("#message").show();
$("#message").fadeIn(400).html('<span">Response false</span>');
setTimeout(function(){
$("#message").fadeOut("slow", function () {
$("#message").hide();
});
}, 1500);
return false;
}
}
});
return …Run Code Online (Sandbox Code Playgroud) 我有一些表单,它在iframe中上传文件.我想保留它的提交,直到我检查它是否与ajax有效.我怎样才能做到这一点 ?我的代码暂停提交并返回验证结果(当前只是一个返回'result':'true'的虚函数)但是然后不执行'submit'操作.在获得响应200状态后显示响应数据需要~2秒是否正常?
这是我的HTML:
<div id="message" style="background:black; width:400px; height:80px; display:none; color:white;">
</div>
<h1>Submit</h1>
<form action="{{upload_url}}" target="upload_target" method="POST" id="file_upload_form" enctype="multipart/form-data">
{% render_upload_data upload_data %}
<table>{{ form }}</table>
<p>
<input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
</p>
<p>
<input type="submit" id="file_upload_submit" value="Submit" />
</p>
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
Run Code Online (Sandbox Code Playgroud)
JS:
$(function() {
$('#file_upload_submit').click(function(e){
e.preventDefault();
var fileUploadForm = document.getElementById('file_upload_form');
$.ajax({
type: "POST",
url: '/artifact/upload/check-form/',
data: 'foo=bar',
dataType: "json",
success: function(data){
if(data['result'] == "true"){
$("#message").show();
$("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
setTimeout(function(){
$("#message").fadeOut("slow", function () { …Run Code Online (Sandbox Code Playgroud) 我有一个表单,将文件中的文件上传到远程服务器.因此,在提交url服务器返回json数据与操作结果,我的iframe捕获.
{'result': 'true' or 'false'}
Run Code Online (Sandbox Code Playgroud)
现在我想将这个json作为我的iframe的回调来检索.我知道我需要jsonp来实现这一点,因为它是一个跨站点调用.这是我的函数,其中包含来自IBM网站的示例代码:
function fileUploadFunction(){
var fileUploadForm = $('#file_upload_form');
fileUploadForm.attr('action', uploadURL);
fileUploadForm.submit();
$('#upload_target').load(function () {
alert("IFrame loaded");
$.getJSON(uploadUrl+"&callback=?", function(data) {
alert("Symbol: " + data.symbol + ", Price: " + data.price);
});
});
};
Run Code Online (Sandbox Code Playgroud)
但这里几乎没有出现问题.首先 - 我的uploadUrl只是"http:// something /".我是否需要它来支持带$callback=后缀的通话?
其次 - 服务器仅在文件上传时给出响应.所以我需要获取存储在我的iframe中的结果,而不是指定的url.怎么解决这个?
这是链接.注意表单中隐藏的iframe.服务器的结果显示在那里.:
http://ntt.vipserv.org/artifact/
编辑
我以前尝试过:
$('#upload_target').load(function () {
var ret = frames['upload_target'].document.getElementsByTagName("body")[0].innerHTML;
var data = eval("("+ret+")");
});
Run Code Online (Sandbox Code Playgroud)
但它引发了"权限被拒绝"的错误.
我有一个以下测试文件:
Jon Smith 1980-01-01
Matt Walker 1990-05-12
Run Code Online (Sandbox Code Playgroud)
解析此文件的每一行,使用(姓名,姓氏,生日)创建对象的最佳方法是什么?当然这只是一个样本,真正的文件有很多记录.
我有来自facebook的朋友列表的示例回复:
[{u'uid': 513351886, u'name': u'Mohammed Hossein', u'pic_small': u'http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs643.snc3/27383_513351886_4933_t.jpg'},
{u'uid': 516583220, u'name': u'Sim Salabim', u'pic_small': u'http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs348.snc4/41505_516583220_5681339_t.jpg'}]
Run Code Online (Sandbox Code Playgroud)
我如何解析这个列表编码字典的关键字到ascii?我尝试过这样的事情:
response = simplejson.load(urllib.urlopen(REST_SERVER, data))
for k in response:
for id, stuff in k.items():
id.encode("ascii")
logging.debug("id: %s" % id)
return response
Run Code Online (Sandbox Code Playgroud)
但是编码的密钥没有保存,因此我仍然得到unicode值.
我有一个程序在一个JTextArea中获取带有文件路径的输入字符串,然后将其内容加载到第二个JTextArea.问题是当使用JTextArea时,我无法添加一个actionListener,它将在离开此字段时在第二个JTextArea中加载内容.如何解决这个问题?
protected JTextArea inputField, outputField;
public Main(){
super(new BorderLayout());
inputField = new JTextArea(5, 20);
outputField = new JTextArea(2, 20);
//inputField.addActionListener(this);
inputField.setEditable(false);
JScrollPane scroller2 = new JScrollPane(inputField);
JScrollPane scroller1 = new JScrollPane(outputField);
this.add(scroller1, BorderLayout.WEST);
this.add(scroller2, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent evt) {
String text = inputField.getText();
(loading contents of file)
}
Run Code Online (Sandbox Code Playgroud) 我现在已经挣扎了一段时间而没有任何成功.使用jquery设置样式不起作用,与facebook iframe之后相同.有没有办法执行此任务?
<iframe src="http://www.facebook.com/plugins/like.php?&href=http%3A%2F%2Fwww.example.com&layout=standard&show_faces=false&width=400&action=like&font=tahoma&colorscheme=light&height=23"
scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:400px; height:23px;" allowTransparency="false"></iframe>
<style>
.connect_widget_not_connected_text, .connect_widget_connected_text {
color:white;
}
.connect_widget_connected_text a {
color:#3B5998;
}
</style>
<script type="text/javascript">
$(function() {
$('.connect_widget_not_connected_text').css('color', 'white');
});
</script>
Run Code Online (Sandbox Code Playgroud) 我有一个标签列表:
>>> tags_list
['tag1', 'second tag', 'third longer tag']
Run Code Online (Sandbox Code Playgroud)
如何用"+"替换列表中每个元素中的空格?我试图用正则表达式做这个,但每个字符串保持不变:
for tag in tags_list:
re.sub("\s+" , " ", tag)
Run Code Online (Sandbox Code Playgroud)
我的做法有什么问题?
编辑:
是的,我忘了提到,在每个标签中的单词之间我们可以有多个空格以及标签可以以空格开头或结尾,因为它们是用逗号分隔的字符串解析的,带有split(","):("First标签,第二个标签,第三个标签").抱歉没有足够精确.
我试图将另一个参数传递给我的表单,这是一个与ForeignKey关系的对象.但是,__init__() got an unexpected keyword argument 'parent'当我非常确定可以向表单发送其他参数时__init__(即这里:简单形式不验证),dunno为什么会返回表单.我错了吗 ?
def add_video(request):
parent = ParentObject.objects.all()[0]
if request.method == 'POST':
form = VideoForm(data=request.POST, parent=parent)
if form.is_valid():
form.save()
next = reverse('manage_playforward',)
return HttpResponseRedirect(next)
else:
form = VideoForm()
class VideoForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
try:
self.parent = kwargs.pop['parent']
logging.debug(self.parent)
except:
pass
super(VideoForm, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud) 我有一个以下形式的字符串:
"425x344"
现在我想将此字符串中的第一个和第二个数字解析为两个单独的变量.我怎样才能做到这一点 ?我创建了这个正则表达式:
regex = re.compile(r"^(\d+x\d+)")
检查字符串形式是否正确.但接下来呢?
python ×4
django ×3
ajax ×2
form-submit ×2
iframe ×2
java ×2
jquery ×2
regex ×2
string ×2
coding-style ×1
cross-domain ×1
css ×1
dictionary ×1
django-forms ×1
encoding ×1
facebook ×1
file-upload ×1
javascript ×1
jsonp ×1
jtextarea ×1
kwargs ×1
match ×1
parsing ×1
replace ×1
response ×1
swing ×1
unicode ×1
validation ×1
whitespace ×1