Printing a JS variable in a value=“” attribute

前端 未结 3 829
自闭症患者
自闭症患者 2021-01-27 11:45

I\'m trying to implement a Paypal button with a dynamic value. When I enter, for example, value=\"300.00\", the button works fine.

However, if I do it my way, the button

相关标签:
3条回答
  • 2021-01-27 12:10

    It doesn't work because you have unclosed tags.
    Notice here that you opened another tag without closing the other tag.

    <*input type="hidden" name="amount" value="<*script

    To achieve what you want, you need to write the entire block using JS

    <script>
    document.write('<input type="hidden" name="amount" value="' + price + '.00">');
    </script>
    
    0 讨论(0)
  • 2021-01-27 12:16

    In javascript:

    document.write('<input type="hidden" name="amount" value="' + price + '">')
    

    Or better yet

    var div = document.createElement('div');
    div.innerHTML = '<input type="hidden" name="amount" value="' + price + '">';
    document.body.appendChild(div);
    

    DEMO

    0 讨论(0)
  • 2021-01-27 12:22
    document.getElementsByName('amount')[0].value = price;
    

    This will find the "button" and set its value to the value of the price variable. I assume that the "button" already is somewhere on the page.

    0 讨论(0)
提交回复
热议问题