普通网友 2015-06-14 20:02
浏览 38
已采纳

从具有相同类的同一页面中的特定表单获取提交

i trying get the submit from a form specific with jquery and alert the value of a field type hiden that contains the a value id; i tried this. but he is getting ever same id value. here are the code jquery

var form = $(".form");

form.submit(function() {
    alert(form.find('#id').val());
    return false;
});

and here code html

<div>
    <form  action="" method="post" class="form">
        <input type="hidden" id="id" name="id" value="1" />
        <button class="submit"  type="submit" name="enviar" id="enviar">Enviar</button>
    </form>
<div/>
<div>
    <form  action="" method="post" class="form">
        <input type="hidden" id="id" name="id" value="2" />
        <button class="submit"  type="submit" name="enviar" id="enviar">Enviar</button>
    </form>
<div/>
<div>
    <form  action="" method="post" class="form">
        <input type="hidden" id="id" name="id" value="3" />
        <button class="submit"  type="submit" name="enviar" id="enviar">Enviar</button>
    </form>
<div/>

which my wrong

  • 写回答

4条回答 默认 最新

  • duan02143 2015-06-14 20:05
    关注

    You are on the right track. But you need to use $(this) instead of form

    The reason it was alerting the first value before is because when you do $('.form').val() it will get the first value by default, even if there are multiple elements. But, when you use $(this), it will get the specific .form that was submitted.

    Finally, avoid using the same id for multiple elements. You can use an alternative such as classes. See Why is it a bad thing to have multiple HTML elements with the same id attribute?

    var form = $(".form");
    
    form.submit(function() {
        console.log($(this).find('.my-class').val());
        return false;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <div>
        <form  action="" method="post" class="form">
            <input type="hidden" class="my-class" name="id" value="1" />
            <button class="submit"  type="submit" name="enviar" id="enviar">Enviar</button>
        </form>
    <div/>
    <div>
        <form  action="" method="post" class="form">
            <input type="hidden" class="my-class" name="id" value="2" />
            <button class="submit"  type="submit" name="enviar" id="enviar">Enviar</button>
        </form>
    <div/>
    <div>
        <form  action="" method="post" class="form">
            <input type="hidden" class="my-class" name="id" value="3" />
            <button class="submit"  type="submit" name="enviar" id="enviar">Enviar</button>
        </form>
    <div/>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)