2011-05-03 49 views
0

如何使用變量輸入參數到動作標籤? 例如:html裏面的Javascript

var actionurl="some url" 
windowhandle.document.write('<form name=sample action=actionurl method="post" </form>'); 
windowhandle.document.sample.submit(); 

這是行不通的。我得到404頁未找到錯誤。請給我一種替代方法

+0

感謝大家。它的工作很好:) – Suki 2011-05-03 14:52:55

+0

優秀。請接受答案:)請參閱每個人左側的勾號。 – 2011-05-04 09:20:30

回答

4

您沒有在您的表單中設置actionurl的值。試試這個:

windowhandle.document.write('<form name=sample action=' + actionurl + 'method="post"> </form>'); 

編輯:你也缺少了「>」我現在已經增加。

+0

不要忘記在「發佈」之後,就像這樣:'

' – onitake 2011-05-03 14:33:49

+0

@onitake - 好的發現,我修改了。 – 2011-05-03 14:35:23

1

試試這個:

var actionurl="some url" 
windowhandle.document.write('<form name=sample action='+ actionurl +' method="post" </form>'); 
windowhandle.document.sample.submit(); 
0

你可以使用Python的.format()功能:

String.prototype.format = function() { 
    var str = this; 
    var i = 0; 
    var len = arguments.length; 
    var matches = str.match(/{}/g); 
    if(!matches || matches.length !== len) { 
     throw "wrong number of arguments"; 
    } 
    while(i < len) { 
     str = str.replace(/{}/, arguments[i]); 
     i++; 
    } 
    return str; 
}; 

只是,代碼粘貼到你的腳本,它可以讓你使用該功能。

Python的.format()是乾淨的字符串操作有用:

'I {} a {}'.format('am', 'string') 
// I am a string 

它只是取代了括號子與函數的參數。

現在,你的新代碼將是這樣的:

var actionurl="some url" 
windowhandle.document.write('<form name="sample" action="{}" method="post"></form>'.format(actionurl)); 
windowhandle.document.sample.submit(); 

一定要使用周圍的屬性報價!

+0

他沒有使用Python ... – Andrea 2011-05-03 14:32:57

+0

Python的.format()?你能澄清一下嗎? :) – rzetterberg 2011-05-03 14:33:51

+0

我知道。它是一個來自Python的'.format()'的JavaScript實現。我雖然OP想要一種方法來插入字符串而不使用加號。 – Blender 2011-05-03 14:34:17