最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting a form via innerHTML? - Stack Overflow

programmeradmin2浏览0评论

I'm having a little trouble setting a form using inner HTML:

document.getElementById("Button").innerHTML='<form action="add.php" method="post" onSubmit="track('P1');">'+
'<input type="hidden" name="add" value="true"> '+
'<input type="hidden" name="item" value="P1"> '+
'<input type="hidden" name="pID" value="3"> '+
'<input type="hidden" name="qty" value="1"> '+      
'<input name="image" type="image" onMouseOver="this.src='/img/shop/r_addbasket.png'" '+
'onMouseOut="this.src='/img/shop/addbasket.png'" '+
'value="Add to Basket" src="/img/shop/addbasket.png" alt="AddtoBasket"></form>';

I assume its because I've got some ' inside the form thats throwing it off. I tried using an escape character but didn't work.

TIA

I'm having a little trouble setting a form using inner HTML:

document.getElementById("Button").innerHTML='<form action="add.php" method="post" onSubmit="track('P1');">'+
'<input type="hidden" name="add" value="true"> '+
'<input type="hidden" name="item" value="P1"> '+
'<input type="hidden" name="pID" value="3"> '+
'<input type="hidden" name="qty" value="1"> '+      
'<input name="image" type="image" onMouseOver="this.src='/img/shop/r_addbasket.png'" '+
'onMouseOut="this.src='/img/shop/addbasket.png'" '+
'value="Add to Basket" src="/img/shop/addbasket.png" alt="AddtoBasket"></form>';

I assume its because I've got some ' inside the form thats throwing it off. I tried using an escape character but didn't work.

TIA

Share Improve this question asked Nov 11, 2012 at 20:19 James MVJames MV 8,72719 gold badges68 silver badges98 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

try this, you didnt escape the ' when using it in a different context

document.getElementById("Button").innerHTML='<form action="add.php" method="post" onSubmit="track(\'P1\');">'+
'<input type="hidden" name="add" value="true"> '+
'<input type="hidden" name="item" value="P1"> '+
'<input type="hidden" name="pID" value="3"> '+
'<input type="hidden" name="qty" value="1"> '+      
'<input name="image" type="image" onMouseOver="this.src=\'/img/shop/r_addbasket.png\'" '+
'onMouseOut="this.src=\'/img/shop/addbasket.png\'" '+
'value="Add to Basket" src="/img/shop/addbasket.png" alt="AddtoBasket"></form>';

Don't escape anything. You don't need to escape XML:

    var xml = <form action="add.php" method="post" onSubmit="track('P1');">
    <input type="hidden" name="add" value="true" /> 
    <input type="hidden" name="item" value="P1" /> 
    <input type="hidden" name="pID" value="3" /> 
    <input type="hidden" name="qty" value="1" />     
    <input name="image" type="image" onMouseOver="this.src='/img/shop/r_addbasket.png'" 
    onMouseOut="this.src='/img/shop/addbasket.png'"
    value="Add to Basket" src="/img/shop/addbasket.png" alt="AddtoBasket" />
</form>
    document.getElementById("Button").innerHTML = xml;

Make sure to close the input tag (as I did above). This is cleaner and easier to read than trying to add the escape characters.

发布评论

评论列表(0)

  1. 暂无评论