-4

I have a form below, which in the "select option" will trigger the function selectType(), I found that the function selectType() has been ran, because the alert works and show in the screen, then the "document.getElementById("selectType").submit();" seems won't works, because it wont go to the url I expected.

Edited at 2017/02/26 : Thanks for the answer.

I found that if the form inside "@using (Html.BeginForm())", then the "document.getElementById("selectType").submit();" won't works, if the form is outside the "@using (Html.BeginForm())", it works.

    <script>
        function selectType() {
            alert("aa");
            document.getElementById("selectType").submit();
        }
    </script>

    <form id="selectType" action="/TRecords/Audit">
        <input type="text" name="id" class="dateInput" value=@ViewBag.SId hidden>
        <select onchange="selectType();" name="auditType" class="form-control">
            <option>Select Type</option>
            <option value=1>A</option>
            <option value=2>B</option>
            <option value=3>C</option>
            <option value=4>D</option>
        </select>
    </form>
kaihong
  • 3
  • 5

4 Answers4

1

Just change 'name' to 'id'.

<form id="selectType" action="/TRecords/Audit">

In order to use 'getElementById' method your need to access using 'id' attribute.

RK_Aus
  • 886
  • 1
  • 11
  • 18
1

You almost got it right but this line:

<form name="selectType" action="/TRecords/Audit">.

Need to be change for that:

<form id="selectType" action="/TRecords/Audit">

  • Please try and provide guidance on why the line needs to be changed so that @kaihong can better understand their mistake. Thanks for contributing! – jon3laze Apr 27 '17 at 04:04
0

"document.getElementById("selectType").submit();" seems won't works

Because you have set name of form to be selectType and not id.

  1. Either set <form id="selectType">..</form>
  2. Or if name is must, then use document.getElementsByName('selectType')[0].submit();
Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
0

Thanks for the answer.

I found that if the form inside @using (Html.BeginForm()) then the document.getElementById("selectType").submit(); won't work. If the form is outside @using (Html.BeginForm()) it works.

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
kaihong
  • 3
  • 5