简体   繁体   中英

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

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!

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

您可以使用document.querySelector或在旧浏览器上模拟它的各种JavaScript库之一来查找隐藏的输入元素,然后使用其form属性来访问表单并调用submit方法。

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM