Note that there are some explanatory texts on larger screens.

plurals
  1. POButton calling javascript function error
    primarykey
    data
    text
    <p>In my view, I want two buttons to populate three hidden fields of a form and submit it. It's not working, and I can't know why. Any ideas?</p> <p>I have the following code:</p> <pre><code>&lt;script language="javascript" type="text/javascript"&gt; function FillAndSubmit(thisid) { $('input[name=First]').val(thisid); $('input[name=Second]').val(@Model.ID); $('input[name=Third]').val(@ViewBag.Something); document.form.submit(); } &lt;/script&gt; </code></pre> <p>A have a form with invisible fields:</p> <pre><code>@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "form", id = "form" })) { @Html.Hidden("First") @Html.Hidden("Second") @Html.Hidden("Third") } </code></pre> <p>And two buttons:</p> <pre><code>&lt;button class="Button" id="ButtonA" onclick="javascript:FillAndSubmit(this.id);"&gt; CaptionA &lt;/button&gt; &lt;button class="Button" id="ButtonB" onclick="javascript:FillAndSubmit(this.id);"&gt; CaptionB &lt;/button&gt; </code></pre> <p>If I put the buttons inside the scope of the form, it submits but without populating the hidden fields. So, it is just not calling the function. Javascript is working in other pages of the same application. </p> <p><b>ADDED:</b></p> <p>Generated source:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8" /&gt; &lt;title&gt;Title&lt;/title&gt; &lt;link href="/Content/Site.css" rel="stylesheet" type="text/css" /&gt; &lt;script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script language="javascript" type="text/javascript"&gt; function Botao(thisid) { $('input[name=First]').val(thisid); $('input[name=Second]').val(41); $('input[name=Third]').val(10/5/2012 1:58:02 PM); document.form.submit(); } &lt;/script&gt; ... Some div's with some text ... &lt;form action="/Controller/Action/" id="form" method="post" name="form"&gt;&lt;input id="First" name="First" type="hidden" value="" /&gt;&lt;input data-val="true" data-val-required="The DateTime field is required." id="Second" name="Second" type="hidden" value="10/5/2012 1:58:02 PM" /&gt;&lt;input id="Third" name="Third" type="hidden" value="" /&gt;&lt;/form&gt; &lt;button class="Button" id="ButtonA" onclick="javascript:Botao(this.id);"&gt; CaptionA &lt;/button&gt; &lt;button class="myButton" id="ButtonB" onclick="javascript:Botao(this.id);"&gt; CaptionB &lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload