asp.net mvc - Button calling javascript function error - Stack Overflow

In my view, I want two buttons to populate three hidden fields of a form and submit it. It's not w

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?

I have the following code:

<script language="javascript" type="text/javascript">
    function FillAndSubmit(thisid) {
        $('input[name=First]').val(thisid);
        $('input[name=Second]').val(@Model.ID);
        $('input[name=Third]').val(@ViewBag.Something);
        document.form.submit();
    }
</script>

A have a form with invisible fields:

@using (Html.BeginForm(null, null, FormMethod.Post, 
                       new { name = "form", id = "form" }))
{
    @Html.Hidden("First")
    @Html.Hidden("Second")
    @Html.Hidden("Third")
}

And two buttons:

<button class="Button" id="ButtonA" onclick="javascript:FillAndSubmit(this.id);">
    CaptionA
</button>
<button class="Button" id="ButtonB" onclick="javascript:FillAndSubmit(this.id);">
    CaptionB
</button>

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.

ADDED:

Generated source:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Title</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>

<body>
    <script language="javascript" type="text/javascript">
        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();
        }
</script>

...
Some div's with some text
...


<form action="/Controller/Action/" id="form" method="post" name="form"><input id="First" name="First" type="hidden" value="" /><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" /><input id="Third" name="Third" type="hidden" value="" /></form>        
    <button class="Button" id="ButtonA" onclick="javascript:Botao(this.id);">
        CaptionA
    </button>

    <button class="myButton" id="ButtonB" onclick="javascript:Botao(this.id);">
        CaptionB
    </button>

    </body>
</html>

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?

I have the following code:

<script language="javascript" type="text/javascript">
    function FillAndSubmit(thisid) {
        $('input[name=First]').val(thisid);
        $('input[name=Second]').val(@Model.ID);
        $('input[name=Third]').val(@ViewBag.Something);
        document.form.submit();
    }
</script>

A have a form with invisible fields:

@using (Html.BeginForm(null, null, FormMethod.Post, 
                       new { name = "form", id = "form" }))
{
    @Html.Hidden("First")
    @Html.Hidden("Second")
    @Html.Hidden("Third")
}

And two buttons:

<button class="Button" id="ButtonA" onclick="javascript:FillAndSubmit(this.id);">
    CaptionA
</button>
<button class="Button" id="ButtonB" onclick="javascript:FillAndSubmit(this.id);">
    CaptionB
</button>

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.

ADDED:

Generated source:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Title</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>

<body>
    <script language="javascript" type="text/javascript">
        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();
        }
</script>

...
Some div's with some text
...


<form action="/Controller/Action/" id="form" method="post" name="form"><input id="First" name="First" type="hidden" value="" /><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" /><input id="Third" name="Third" type="hidden" value="" /></form>        
    <button class="Button" id="ButtonA" onclick="javascript:Botao(this.id);">
        CaptionA
    </button>

    <button class="myButton" id="ButtonB" onclick="javascript:Botao(this.id);">
        CaptionB
    </button>

    </body>
</html>
Share Improve this question edited May 10, 2012 at 17:35 Lucas Reis asked May 10, 2012 at 17:15 Lucas ReisLucas Reis 74111 silver badges22 bronze badges 6
  • 1 Please show us the generated source. – SLaks Commented May 10, 2012 at 17:19
  • Does the browser's JS console show any error?. Have you tried to remove the javascript: part of your onclick attributes since is not needed?. Have you tried to debug with a console.log to see if the function is called?. Have you tried to call the function directly from the Browser's JS console? – fguillen Commented May 10, 2012 at 17:22
  • Removing the javascript: didn't do anything. I will check the JS console now. – Lucas Reis Commented May 10, 2012 at 17:26
  • Unless @mokdel.ID is a number you will get an unknow identifier at val(@Model.ID) same for the line below – Rune FS Commented May 10, 2012 at 17:28
  • Model.ID is a number, and ViewBag.Something is a DateTime. – Lucas Reis Commented May 10, 2012 at 17:31
 |  Show 1 more ment

2 Answers 2

Reset to default 3

This needs quotes:

$('input[name=momentoDaConsulta]').val("10/5/2012 1:58:02 PM");

JQuery's .val(value) accepts either string or array of strings, place quotes around @Model.ID and @ViewBag.Something:

    $('input[name=Second]').val("@Model.ID");
    $('input[name=Third]').val("@ViewBag.Something");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745564409a4633306.html

相关推荐

  • asp.net mvc - Button calling javascript function error - Stack Overflow

    In my view, I want two buttons to populate three hidden fields of a form and submit it. It's not w

    20小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信