Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
    
have you tried location.href = ... ? or location.replace(...) –  Romko Oct 8 '13 at 9:45
    
Please add your browser rendered html and javascript –  Murali Oct 8 '13 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. –  Kamil T Oct 8 '13 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 –  Murali Oct 8 '13 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"). –  Kamil T Oct 8 '13 at 10:54

3 Answers 3

up vote 1 down vote accepted

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.
});
share|improve this answer
1  
it is good but for the parameters how can i add it –  Lamloumi Afif Oct 8 '13 at 10:05
1  
@Lamloumi good point, I updated by answer. By the way, does the javascript: prefix work in the onclick attribute? –  Henk Mollema Oct 8 '13 at 10:14
    
please see my edit –  Lamloumi Afif Oct 8 '13 at 10:44
1  
@Lamloumi is this submit button inside a for(each) loop, ie. is it on the page multiple times? –  Henk Mollema Oct 8 '13 at 10:48
1  
@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. –  Henk Mollema Oct 8 '13 at 11:33

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

share|improve this answer
    
the problem is that i can't reach the javascript function not inside it –  Lamloumi Afif Oct 8 '13 at 9:59
1  
@Lamloumi, Updated the answer. Now you could able to reach your js function –  Murali Oct 8 '13 at 10:04
    
the same result , i can't reach the js function –  Lamloumi Afif Oct 8 '13 at 10:10
    
please see my edit –  Lamloumi Afif Oct 8 '13 at 10:44

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');"
share|improve this answer
    
the same result !!! see my edit plz –  Lamloumi Afif Oct 8 '13 at 9:46
    
for the apostrophes i didn't understand you sorry –  Lamloumi Afif Oct 8 '13 at 9:51
    
please see my edit –  Lamloumi Afif Oct 8 '13 at 10:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.