表格有两个按钮

时间:2017-11-23 12:29:11

标签: javascript jquery forms

我正在尝试创建多个具有两个按钮的表单,每个表单都将表单提交到不同的脚本,一个通过ajax,第二个只提交表单。

def check_for_value(input):
   if input==b:
      return True

result=filter(check_for_value,a )
print result

首先,我试图获取当前的表单,但我有问题。 <?php foreach($objects as $object) : ?> <div class="card-body"> <form id="edit-form" action="#" method="POST"> <input name="subject" value="<?=$object['subject']?>" type="text" id="title" class="input-xxlarge"> <textarea id="content" name="content" rows="25"><?=$object['content']?></textarea> <button type="button" id="send-button" class="btn btn-primary">Send</button> <input type="submit" id="submit-button" value="Submit"/> </form> </div> <?php endforeach; ?> 仅在第一个表单上显示某些内容,如果我点击其他表单中的按钮,那么它将无效。

console.log

是因为我的按钮对每个表单都有相同的ID吗?

2 个答案:

答案 0 :(得分:2)

您不能在同一页面上多次使用ID。尝试为该案例使用一个类。同样如评论中所述,使用e.preventDefault();来停止活动。

$(this)会导致#send-button成为目标。要访问表单,您需要找到最近的form元素,如下所示:

$form = $(this).closest('form');

答案 1 :(得分:0)

HTML:

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

JS:

<form method="POST" action="#">
  <input type="text">
  <button type="button">send</button>
  <input type="submit" value="submit" />
</form>

<form method="POST" action="#">
  <input type="text">
  <button type="button">send</button>
  <input type="submit" value="submit" />
</form>

这会在您网页上的每个表单上添加活动,$("form").each(function() { var form = this; $(form).find('button').on('click', function(e) { e.preventDefault(); console.log(form); console.log(this); }) }); 将通过脚本提交表单,而button只会提交。这里还有fiddle

一起玩