简体   繁体   中英

jQuery .html method returns broken code after form submit

I have the following HTML which I'm fetching via the DOM and inserting it into a hidden form in wordpress:

    <div class="main_class">
        <div class="itemRow row-0 odd" id="cartItem_SCI-1">
            <div class="item-thumb">
                <img src="http://example.com/248916">
            </div>
        </div>
        <div class="itemRow row-0 odd" id="cartItem_SCI-1">
            <div class="item-thumb">
                <img src="http://example.com/248915">
            </div>
        </div>
        <div class="itemRow row-0 odd" id="cartItem_SCI-1">
            <div class="item-thumb">
                <img src="http://example.com/248917">
            </div>
        </div>
    </div>

I'm using the following code:

    $('body').on('click', function(){
        var temp = $('.main_class').html();
        $('myform').val(temp);
    });

The problem is after I submit the form and check out the actual form results, this is what I get:

    &lt;div class="main_class"&gt;
        &lt;div class="itemRow row-0 odd" id="cartItem_SCI-1"&gt;
            &lt;div class="item-thumb"&gt;
                &lt;img&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="itemRow row-0 odd" id="cartItem_SCI-1"&gt;
            &lt;div class="item-thumb"&gt;
                &lt;img&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="itemRow row-0 odd" id="cartItem_SCI-1"&gt;
            &lt;div class="item-thumb"&gt;
                &lt;img&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

For some reason, the image src attributes aren't getting saved. I have tried a lot of different methods such as innerHTML, clone() and other methods but none seem to work in getting the image src attributes saved.

My question is, how can I get the image src attributes saved properly in this case.

Thanks

The most probable explanation to this activity is that your server-side softvare is escaping dangerous characters into html entities to prevent XSS attacks. If you only want ot get src attributes of images sent to server you can do so by using jQuery's .attr('src') on the $('.main_class img') object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM