0

i have an asp.net application in which i have to custmize the action click in submit button of a form :

the Script

<script>
  $(function(){
    function result(arr, identificateur,Pages) {
   var f1 = $('input[name="reception' + arr + '"]').val();
   var f2 = $('input[name="client' + arr + '"]').val();
   var f3 = $('input[name="cloture' + arr + '"]').val();
   var f4 = $('input[name="tag' + arr + '"]').val();
   location = "@Url.Content("/Pages/Index")?identificateur=" + identificateur + "&Pages=" + Pages + "&_reception=" + f1 + "&_cloture=" + f3 + "&_client=" + f2 + "&_tag=" + f4;
    }});
  </script>

then, the form:

 <input type="submit" onclick="result(@i.ToString(),@Model[1][i].Id ,"1");" value="Enregistrer"/>

When i click into the submit button, nothing is happend and i'm not redirected to the action Index.

What are the reasons of this problem? How can i fix it?

Edit

<input type="submit" id="Enregistrer" value="Enregistrer" data-arr="@i.ToString()" data-identificateur="@Model[1][i].Id"  />





<script>
    $(function () {
            $('#Enregistrer').click(function () {
            var self = $(this); // This is the submit button
            var identificateur = self.data('identificateur');
            var arr = self.data('arr');
            var f1 = $('input[name="reception' + arr + '"]').val();
            var f2 = $('input[name="client' + arr + '"]').val();
            var f3 = $('input[name="cloture' + arr + '"]').val();
            var f4 = $('input[name="tag' + arr + '"]').val();
             document.location.href = "@Url.Content("/Pages/Index")?identificateur=" + identificateur + "&Pages=1&_reception=" + f1 + "&_cloture=" + f3 + "&_client=" + f2 + "&_tag=" + f4;
        });
    });
  </script>

The problems :

  1. the redirection didn't work
  2. Only in the first tr i can reach the js function
8
  • have you tried location.href = ... ? or location.replace(...) Commented Oct 8, 2013 at 9:45
  • Please add your browser rendered html and javascript Commented Oct 8, 2013 at 9:46
  • 1
    Do you get any console errors? Does your code even reach the result() function? Try adding console.log("IM IN RESULT()"); in the result() and check if it show in console. Commented Oct 8, 2013 at 9:49
  • 1
    @Lamloumi, What was an equivalant js/html code rendered for your razor syntax. Please check the viewsource or response with firebug if it is a Ajax response. Like to see wat was actually rendered Commented Oct 8, 2013 at 10:51
  • 1
    You can't use many elements with the same id. Use the class instead. The class selector is $(".className") instead of $("#id"). Commented Oct 8, 2013 at 10:54

3 Answers 3

2

Pass the string parameter with quote ' like 'someVal', else you will get undefined error

Changing this below line

 onclick="result(@i.ToString(),@Model[1][i].Id ,"1");"

to

onclick="result('@i.ToString()','@Model[1][i].Id' ,"1");"

will make a call to your javascript function, otherwise all rendered string will be considered as variable like below and you will get someval,someId undefined error

onclick="result(someval,someId ,"1");"

Also use location.href for navigation

Sign up to request clarification or add additional context in comments.

3 Comments

the problem is that i can't reach the javascript function not inside it
@Lamloumi, Updated the answer. Now you could able to reach your js function
the same result , i can't reach the js function
1

I think I spotted two errors:

location

should be

location.href

And the second, try to prefix the function with javascript::

<input type="submit" onclick="javascript:result(@i.ToString(),@Model[1][i].Id ,"1");" value="Enregistrer"/>


Or even better, use unobtrusive javascript with html5 data- attributes.

<input type="submit" id="Enregistrer" value="Enregistrer" data-reception="@i.ToString()" />

With the following JavaScript:

$('#Enregistrer').click(function () {
    var self = $(this); // This is the submit button
    var reception = self.data('reception');   // Get the data from the attributes.
});

6 Comments

@Lamloumi good point, I updated by answer. By the way, does the javascript: prefix work in the onclick attribute?
@Lamloumi is this submit button inside a for(each) loop, ie. is it on the page multiple times?
yes sorry @Henk i change the code : i proceed by class because i'm in a for loop. only the problem of redirection stays
@Lamloumi is the generated url correct? Try to use @Url.Action("Index", "Pages", new { identificateur = identificateur, pages = 1, _reception = f1 }) with all the other parameters.
|
1

Put the definition of result() function inside $(function(){ /* put it here */ }); instead of simple <script>.

Also remember to put apostrophes instead of quotation marks in onclick="result(@i.ToString(),@Model[1][i].Id ,"1");"

when you pass the ""`.

It should be

onclick="result(@i.ToString(),@Model[1][i].Id ,'1');"

1 Comment

for the apostrophes i didn't understand you sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.