How do I reset the HTML-fields in a text string, which is just a part of a form? (preferably with jQuery) [duplicate]
Possible Duplicate:
How do I clear all the fields in a variable html content? (preferably with jQuery)
The answer of this question is not find in this question: Blank out a form with jQuery, since I have no form ID to connect to.
I hav开发者_Python百科e a text string with a value of a HTML code snippet that has some form elements within, with pre filled values. All fields that are found in this code snippet (only) must be cleared/reset.
You could just create a form, populate it with your string, and then reset it, like so:
var myForm = $('<form>').html(your_string).
find(':input').not(':button, :submit, :reset, :hidden').
val('').removeAttr('checked').removedAttr('selected');
Then it's just a matter of accessing the innerHTML
of myForm
.
Assuming you have a text string like this:
var txt = '<form id="search" action="/search" method="get" autocomplete="off"><div><input autocomplete="off" name="q" type="text" value="sample"></div></form>'
var $form = $(txt);
$form[0].reset();
var resetTxt = $form.html();
精彩评论