2013-01-12 26 views
0

有沒有辦法將輸入字段的查詢部分拆分爲變量,並將值關聯到新的輸入字段?從輸入字段讀取url查詢並將每個var放在新的輸入字段中

比方說,我有一個輸入的字段,如:

<input type="text" name="sUrl" value="http://site.com/index.php?var1=1&var2=1&var3=foo" /> 

我想要實現的是jQuery的轉動查詢到:

<input type="text" name="var1" value="1" /> 
<input type="text" name="var2" value="1" /> 
<input type="text" name="var3" value="foo" /> 

在PHP中,我可以做一個發生爆炸查詢部分,但傷心php!= jquery,什麼是最好的方式來完成這個?

在此先感謝。

+0

是的,無賴。 PHP + jQuery = * asplode *。 Soo值得像50個彩虹獨角獸。可悲,真的。 –

+0

最難的部分實際上是從URL解析請求參數。不要使用正則表達式或字符串操作 - 讓瀏覽器爲您做好工作。這已經在網站上覆蓋過;請參閱http://stackoverflow.com/a/6168370/221061以瞭解我能找到的最佳示例。 –

+0

爲什麼不在PHP之前先輸出html呢?瘋。 – keyboardSmasher

回答

0

試試這個:

<script> 
function myfunction() { 
    var target = $('body'); 
    var input = $('#myinput'); 
    var pathname = input.val(); 

    pathname = pathname.replace('?',''); 
    pathname = pathname.split('&'); 

    $(pathname).each(function() { 
     var request = this.split('='); 
     $('<input>',{type:'text',name:request[0],value:request[1]}).appendTo(target); 
    }); 

    return false; 
} 
</script> 

<form onsubmit='return myfunction();'> 
    <input type='text' id='myinput'> 
</form> 

與實際目標和$('#myinput')你輸入替換$('body')

+1

需要從輸入值拉'路徑名',而不是'位置'對象 – charlietfl

+0

謝謝,@charlietfl。只是編輯了代碼:) –

相關問題