最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I submit a form with no form ID and no submit ID, but known hidden value? - Stack Overflow

programmeradmin2浏览0评论

the situation I'm struggling with is that there are more forms on the page that looks like this (the hiddenId is different in each form):

<form method="post">
<input type="hidden" name="hiddenId" value="111222">
<input type="submit" value="Proceed">
</form>

<form method="post">
<input type="hidden" name="hiddenId" value="111333">
<input type="submit" value="Proceed">
</form>

How do I submit with javascript (I don't mind using jQuery) a specific form that includes hiddenId of a wished value? Thank you for your help!

the situation I'm struggling with is that there are more forms on the page that looks like this (the hiddenId is different in each form):

<form method="post">
<input type="hidden" name="hiddenId" value="111222">
<input type="submit" value="Proceed">
</form>

<form method="post">
<input type="hidden" name="hiddenId" value="111333">
<input type="submit" value="Proceed">
</form>

How do I submit with javascript (I don't mind using jQuery) a specific form that includes hiddenId of a wished value? Thank you for your help!

Share Improve this question edited Jan 12, 2012 at 21:49 Sidd Sidd asked Jan 12, 2012 at 21:38 Sidd SiddSidd Sidd 951 gold badge1 silver badge4 bronze badges 2
  • 1 why can't you give the forms a class or id? – james Commented Jan 12, 2012 at 21:40
  • name is deprecated, you should use id. never use the same id for more than one element. – Travis J Commented Jan 12, 2012 at 21:44
Add a comment  | 

5 Answers 5

Reset to default 7

Something along these lines should get you started:

var inputs = document.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].type === "hidden" && inputs[i].value === "111333") {
        inputs[i].form.submit();
    } 
}

If you can use jQuery:

$("input[type='hidden'][value='something']").closest("form").submit();

document.forms is an array. document.forms[0] is the first one.

Elements work the same way:

document.forms[0].elements[0].value etc.

You can loop through until you have the value then submit the current form:

document.forms[x].submit()

Access all of the forms using var allForms = document.forms;

loop through them as needed to access inputs

You could use document.querySelector or one of the various JavaScript libraries that emulate it on older browsers to find the hidden input element, then use its form property to access the form and invoke the submit method.

You can add an id to the form and submit button:

<form method="post" id="form1">
<input type="hidden" name="hiddenId" value="111222">
<input type="submit" value="Proceed" id="submit1">
</form>

<form method="post" id="form2">
<input type="hidden" name="hiddenId" value="111333">
<input type="submit" value="Proceed" id="submit2">
</form>

Then use jQuery to submit the form:

$("#submit1").click(function() {
    form1.submit();
}
$("#submit2").click(function() {
    form2.submit();
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论