如果按下按钮,则发送表单

时间:2015-07-09 00:54:03

标签: html forms button

我的表单中有两个按钮(JS正在收听,因此我可以在表单中显示某些字段)。

<div class="btn-group" role="group" aria-label="...">
  <button type="button" class="btn btn-default" name="Distance" id="one_way_button">Distance & Time</button>
  <button type="button" class="btn btn-default" name="Distance" id="stored_button">Stored Address</button>
</div>

我想实际发送表单发布数据中按下的按钮。如果重要的话,我们在铁轨上使用红宝石。不知道我需要做些什么来改变我的html以实际发送按下哪个按钮。

2 个答案:

答案 0 :(得分:1)

您可以在提交之前将按钮名称存储在隐藏输入中

function fnSubmitForm(obj)
{
    document.getElementById("ButtonName").value = obj.value

    document.getElementById("myForm").submit();
}

点击以调用您的上述功能

<div class="btn-group" role="group" aria-label="...">
<input type="hidden" id="ButtonName" name="ButtonName" value="" />
<input type="button" class="btn btn-default" name="Distance" id="one_way_button" onclick="fnSubmitForm(this)" value="Distance & Time" />
<input type="button" class="btn btn-default" name="Distance" id="stored_button" onclick="fnSubmitForm(this)" value="Stored Address" />
</div>

答案 1 :(得分:0)

您只需使用""https://developer.mozilla.org/en-US/docs/Web/API/Event/target)属性即可知道点击了哪个按钮。

event.target
相关问题