简体   繁体   English

javascript onclick无法在表单中工作

[英]javascript onclick not working in a form

i am trying to change text of a span using onclick of javascript but its not working . 我正在尝试使用javascript的onclick更改跨度的文本,但无法正常工作。 Its very simple stuff but i am not sure why its not working 它非常简单,但是我不确定为什么它不起作用

<div class="subscribe_block">
   <h3><span>Stay Connected</span></h3>
   <form method="post" action="" name="subscribeForm">
    <span id="message"></span>
    <p><input type="text" id="name" placeholder="Your Name" /></p>
    <p><input type="text" id="email" placeholder="Email Id" /></p>
    <p><input type="submit" value="subscribe" onClick='document.getElementById("message").value = "Thank you for subscribing";return false;' /></p>
   </form>
  </div>
    <div class="subscribe_block">
   <h3><span>Stay Connected</span></h3>
   <form method="post" action="" name="subscribeForm">
    <span id="message"></span>
    <p><input type="text" id="name" placeholder="Your Name" /></p>
    <p><input type="text" id="email" placeholder="Email Id" /></p>
    <p><input type="submit" value="subscribe" onClick='document.getElementById("message").innerHTML = "Thank you for subscribing";return false;' /></p>
   </form>
  </div>

Use 'innerHTML' instead of 'value' . 使用'innerHTML'代替'value'

On the onclick function, try changing, 在onclick函数上,尝试更改,

'document.getElementById("message").value = "Thank you for subscribing";return false;' 'document.getElementById(“ message”)。value =“谢谢您的订阅”;返回false;'

to

 'document.getElementById("message").innerHTML = "Thank you for subscribing";return false;'

Just change value to innerHTML as 只需将value更改为innerHTML作为

<div class="subscribe_block">
    <h3><span>Stay Connected</span></h3>
    <form method="post" action="" name="subscribeForm">
        <span id="message"></span>
        <p><input type="text" id="name" placeholder="Your Name" /></p>
        <p><input type="text" id="email" placeholder="Email Id" /></p>
        <p><input type="submit" value="subscribe" onClick='document.getElementById("message").innerHTML = "Thank you for subscribing";return false;' /></p>
    </form>
</div>

using InnerHtml will set your Span text but as it will post back so you will be unable to see anything. 使用InnerHtml会设置您的Span文本,但它将回发,因此您将看不到任何内容。 Remove form tag then you will be able to see it. 删除表单标签,您将可以看到它。

<div class="subscribe_block">

Stay Connected 保持联系

<span id="message"></span>
<p><input type="text" id="name" placeholder="Your Name" /></p>
<p><input type="text" id="email" placeholder="Email Id" /></p>
<p><input type="submit" value="subscribe"  onClick='document.getElementById("message").innerHTML= "Thank you for subscribing";return false;'  /></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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