dongzhen6554 2016-07-08 19:51
浏览 19
已采纳

使用javascript [duplicate]点击后更改按钮的文本

This question already has an answer here:

I am new to javascript and I am facing a problem with code:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<form id="add-to-cart-form">
    <div class="cart">
        <input type="hidden" name="product_code" value="prince"/>
        <input type="text" name="name" value="First Form"/>
        <button id="btn_add" type="submit">Add to Cart</button>
    </div>
</form>

<form id="add-to-cart-form">
    <div class="cart">
        <input type="hidden" name="product_code" value="lionel"/>
        <input type="text" name="name" value="Second Form"/>
        <button id="btn_add" type="submit">Add to Cart</button>
    </div>
</form>

   
<script>
    $(document).ready(function(){
        $("#add-to-cart-form").submit(function(e) {

            $("#btn_add").html('Good');
            $("input[name='name']").val('Input change');

            e.preventDefault(); // avoid to execute the actual submit of the form.
        });
    });
</script>

The purpose of my code is to change the submit button when the form is submitted and change of value of the input of the submitted form. But with my code, after submitting the first form, the submit button changes (this is normal) and the value of the input field of the second form changes (not normal). Also after submitting the second form, it does not work at all.

Sorry I tried my best to make the post clear. I am not a native english speaker.

</div>
  • 写回答

1条回答 默认 最新

  • douhuigan8063 2016-07-08 19:55
    关注

    The id should be unique per page
    Try it:

    <form id="add-to-cart-form-first">
        <div class="cart">
            <input type="hidden" name="product_code" value="prince"/>
            <input type="text" name="name" value="First Form"/>
            <button id="btn_add_first" type="submit">Add to Cart</button>
        </div>
    </form>
    
    <form id="add-to-cart-form-second">
        <div class="cart">
            <input type="hidden" name="product_code" value="lionel"/>
            <input type="text" name="name" value="Second Form"/>
            <button id="btn_add_second" type="submit">Add to Cart</button>
        </div>
    </form>
    
    
    <script>
        $(document).ready(function(){
            $("form").submit(function(e) {
    
                $(this).find("[type=submit]").html('Good');
                $(this).find("input[name='name']").val('Input change');
    
                e.preventDefault(); // avoid to execute the actual submit of the form.
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器