admin管理员组

文章数量:1317906

         *@ JAVASCRIPT
             $(document).ready(function()
             {
             }
             )

             function videochange(url) {
                 alert(url)
             }

On the click of this button it wont hit the JavaScript function above. Any idea why? I really need to get that video.Value to a JavaScript function.....

 @foreach(var video in Model.VideoList)
        {           
            var url = video.Value;
         <input type="button" value="@video.Text" onclick="javascript:videochange(url);" />
            <br />
        }
         *@ JAVASCRIPT
             $(document).ready(function()
             {
             }
             )

             function videochange(url) {
                 alert(url)
             }

On the click of this button it wont hit the JavaScript function above. Any idea why? I really need to get that video.Value to a JavaScript function.....

 @foreach(var video in Model.VideoList)
        {           
            var url = video.Value;
         <input type="button" value="@video.Text" onclick="javascript:videochange(url);" />
            <br />
        }
Share Improve this question edited Jan 6, 2014 at 12:16 BenMorel 36.6k51 gold badges205 silver badges336 bronze badges asked Jun 19, 2013 at 13:07 Corey ToolisCorey Toolis 3071 gold badge3 silver badges21 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

I would use JQuery. Assuming you've got version 1.7 or later, the following code should work. It also enforces a better separation between your markup and your JavaScript.

<div class="container">
  @foreach(var video in Model.VideoList) {           
    <input type="button" value="@video.Text" data-url="@video.Value" />
  }
</div>

<script>
  $(document).ready(function(){
    $('.container').on('click', 'input[type="button"]', function(){
      var url = $(this).attr('data-url');
      alert(url);
    });
  });
</script>

Try this

@foreach(var video in Model.VideoList)
{           
    var url = video.Value;
 <input type="button" value="@v.Value" onclick="@String.Format("videochange('{0}')", url)" />
    <br />
}

Try

@foreach(var video in Model.VideoList) {           
    <input type="button" value="@video.Text" onclick="videochange('@video.Value');" />
    <br />
}

本文标签: aspnet mvcCall Javascript onClick not workingStack Overflow