How do I clear all the fields in a variable html content? (preferably with jQuery)

后端 未结 4 1917
天命终不由人
天命终不由人 2021-01-17 01:33

I need to reset all the form fields in an HTML content I saved in a variable. I searched the internet and here too. But none of what I\'ve found has worked yet. It won\'t su

4条回答
  •  不知归路
    2021-01-17 02:14

    This is a bit of a guess as I don't know what your mark-up looks like but jQuery 'find' may not work depending on how the html string is structured - have a look at this question -

    Using jQuery to search a string of HTML

    You could try looping through the 'content' string, then update each form element individually and add them to relevant container, something like -

    $(content).each(function () {
       if (this.nodeName == 'INPUT') $(this).val('');
       if (this.nodeName == 'SELECT') $(this).children('option').prop('selected','');
       $("#moveto").append(this);
    });
    

    Working demo - http://jsfiddle.net/vFMWD/5/

提交回复
热议问题