我需要通过HTTP Post(表单)通过单击将文件发送到多个目的地。是否可以发送包含通过JavaScript提交按钮的HTML表单?
的形式具有提交按钮
<input type="submit" id="button1" name="submit" value="upload file">
action属性和所需的各种不同的隐藏的字段中,通过JavaScript设置。
点击button1后,文件将被发送到第一个目的地,没有问题。
由于提交按钮,我不可能第二次发送表单并获得不同的值。
我不能使用外部按钮像
<button id="button1">upload file</button>
这使问题容易解决(我已经尝试过),但它是不可能在这里。原因是该消息需要这些线在由一个真正的产生结束时提交HTML表单内按钮:
Content-Disposition: form-data; name="submit"
upload file
我的问题:我如何通过JavaScript包括一个提交按钮发送表格?我怎样才能通过JavaScript“点击”一个提交按钮?
这是我的Java脚本文件:
// If frame 1 is loaded, the function prepareUpload() will be executed
$("#frame1").ready(function() {
prepareUpload();
});
function prepareUpload(){
// click-handler is registered to button1
$('#button1').click(function(){
// if clicked...
// "Please Wait" window
$.blockUI({ css: {
border: 'none',
padding: '15px',
backgroundColor: '#000',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
opacity: .5,
color: '#fff'
} });
//register load handler on iframe which triggers uploadData()
$('#frame1').load(function(){
// reload from iframe
uploadData();
});
iTargetNumber = 0;
// call function uploadData()
uploadData();
return true;
});
}
function uploadData() {
var dataSet = data[iTargetNumber];
document.getElementById("statusbar").style.width = (iTargetNumber/data.length * 200) + "px";
// if file is uploaded to all servers...
if (iTargetNumber == data.length) {
document.getElementById("statustext").innerHTML = "done";
// Reload browser window
document.location.reload();
return;
}
// status message
document.getElementById("statustext").innerHTML = "uploading to " + dataSet.sUrl;
//set "action" attribute in HTML form
$('#form1').attr('action', dataSet.sUrl);
//set hidden fields in HTML form
for (var key in dataSet){
$('#' + key).attr('value', dataSet[key]);
}
// HERE IT HANGS after the first run is completed because of the submit button.
//submit HTML form with correct values
$('#form1').submit();
// increment variable +1
iTargetNumber++;
}
// initialize variable
var iTargetNumber = 0;
它挂后的第一种形式发送(因为提交按钮)。
使用
var myForm = document.forms.form1
var emptyForm = document.createElement('form');
emptyForm.submit.apply(myForm);
insted的的
$('#form1').submit();
于事无补了。将所得的消息没有这些行包括:
Content-Disposition: form-data; name="submit"
upload file
一个附加隐藏字段等
<input type="hidden" id="submit" name="submit" value="submit">
,将创建一个内容处置:形状数据; name =“submit”也没有帮助,因为它会在文件的内容之前,最终没有上传文件。
================
问题解决了。该解决方案是JavaScript的Funktion通过按钮
<button id="button1">upload file</button>
形式的内部调用也是一个隐藏的提交按钮
<input type="submit" style="display:none" id="button2" name="submit" value="upload file">
中的JavaScript的Funktion调用这个隐藏提交按钮。现在,它正在毫无问题地工作。
我找不出什么问题,但听起来好像使用AJAX可能更适合您正在尝试做的事情。 http://api.jquery.com/jQuery.ajax/ – BudgieInWA 2010-10-23 20:41:18
我使用jQuery 1.4.2 – Neverland 2010-10-23 20:45:40
你试过'$(“#button1”)。onclick()'?因为,这实际上是用户点击按钮时触发的。另外,我会检查未定义的值,因为你放在元素上的闭包(特别是'iTargetNumber'和'data')。 – FK82 2010-10-24 09:33:59