2014-09-04 112 views
0

我有一個textarea與表單的代碼,用戶將複製並粘貼到他們的網站。用textarea替換一個字符串與另一個文本

在textarea的HTML代碼將是這樣的

UPDATE:注意下面的形式被封閉在一個文本。這將在一個文本

<form action="https://www.xxxxxxx.com/servlet/servlet?encoding=UTF-8" method="POST"> 
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br> 

<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br> 

<input type="submit" name="submit"> 

</form> 

我想表單標籤的URL更改爲https://www.yyyyyyyyy.com/parse.php和改變形式代碼複製到使用jQuery或普通的JavaScript另一個textarea的純文本。以下是完整的HTML

<form> 
<textarea rows="10" cols="80" id="oldCode"> 
    <form action="https://www.xxxxxxx.com/servlet/servlet?encoding=UTF-8" method="POST"> 
    <label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br> 

    <label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br> 

<input type="submit" name="submit"> 

</form> 
</textarea> 
    <button onclick="generateCode()">Generate new Code</button> 
<textarea id="newCode" rows="10" cols="80"> 
</textarea> 
</form> 

回答

2

您可以按以下方式做到這一點,把type="button"的按鈕元素,因爲這每次提交

<button type="button" id="generate" >Generate new Code</button> 

使用下面的jQuery哪裏綁定單擊窗體事件按鈕並替換表單的動作屬性。
注意: - 你可以調用的onclick用相同的代碼,而不是biniding click事件JavaScript函數

$(function(){ 
    $("#generate").click(function(){ 
     var value = $('#oldCode').val(); 
     var div = $('<div></div>'); 
     $(div).append(value); 

     $(div).find('form').attr('action','https://www.yyyyyyyyy.com/parse.php'); 

     $('#newCode').val($(div).html()); 
    }); 
}); 

Demo

0

添加idformmyForm

$('button').click(function(){ 
    var formClone = $('#myForm').clone(); 
    formClone.attr('action', 'https://www.yyyyyyyyy.com/parse.php'); 
    $('#newCode').text(formClone); 
)}; 
+0

表單在textarea中。此表單應視爲文本 – Prady 2014-09-04 06:57:44

0

舉一個id到窗體。

<form action="https://www.xxxxxxx.com/servlet/servlet?encoding=UTF-8" method="POST" id="form"> 
                        ^
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br> 

<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br> 

<input type="submit" name="submit"> 

</form> 

而在jQuery函數

$('#form').attr('action', 'https://www.yyyyyyyyy.com/parse.php '); // set the action attribute to required page 
$('#newCode').html($('#form').html()); // set the html of textarea with id newCode as the html content of form with id form 
+1

問題中的「表單」是__plain text__,而不是HTML元素。 – Teemu 2014-09-04 06:37:22

1

這裏有一個簡單的片斷:

$('button').click(function() { 
    var clone = $($('#oldCode').val()).clone(); 
    clone.attr('action', 'https://www.yyyyyyyyy.com/parse.php'); 
    $('#newCode').val(clone[0].outerHTML); 
}); 

即只需創建textarea值的克隆,更改屬性並將克隆的HTML添加到其他textarea。

A live demo at jsFiddle

相關問題