javascript change value of button

前端 未结 4 519
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 14:18

How do I change the value of this button? I am looking at a tutorial, but only the url seems to change, and not the button.

相关标签:
4条回答
  • 2020-12-19 14:39
    document.form.button.innerHTML = new Date();
    


    EDIT:

    If what you're trying to is to make the text on the button change to the current date when you click it, this is what you want to do:

    <script type="text/Javascript">
        function changeLabel()
        {
            document.getElementById('button').innerHTML = new Date();
        }
    </script>
    
    <button id="button" onclick="changeLabel()">Click Me!</button>
    
    0 讨论(0)
  • 2020-12-19 14:48

    Use innerHTML:

    document.form1.button1.innerHTML=new Date();
    

    UPDATE: Alternative you could define your button like:

    <input name="button" id="button" type="button" value="Click Me!" />
    

    In that case

    document.form.button.value=new Date();
    

    should work as you had expected .

    0 讨论(0)
  • 2020-12-19 14:55

    I added an onclick to your button to change the value with the function.

    If you add onsubmit="return false" to the form tag, it won't refresh the page.

    <form name="form" id="form" onsubmit="return false">
        <button name="button" id="button" onclick="changeValue();" value="before" >Click Me!</button>
    </form>
    
    <script type="text/javascript">
        function changeValue()
        {
            // Changes the value of the button
            document.form.button.value = new Date();
    
            // Changes the text on the button
            document.form.button.innerHTML = new Date();
        }
    </script>
    
    0 讨论(0)
  • 2020-12-19 14:56

    This works for sure...

    onclick="changeValue(this);"
    
    function changeValue(button)
    {
        // Changes the value of the button
        button.value = new Date();
    }
    
    0 讨论(0)
提交回复
热议问题