$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Razor button onclick call only when condition is true - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Razor button onclick call only when condition is true - Stack Overflow

matteradmin16PV0评论

Is it possible to call onclick event on button only when certain condition is met ?

F.e.: I would like to call my onclick event if function call returns number > 0, something like: if(checkNoOfRows() > 0) //call onclickevent

Is it possible to do this inline ?

Here's my button onclick event, which I would like to call only when certain condition is fulfilled:

<button id="btnExportXLSPropsChild" onclick="location.href='@Url.Action("exportUserProp", "UserManagement", new { typeToExport = "CHILD" })'">Export</button>

Is it possible to call onclick event on button only when certain condition is met ?

F.e.: I would like to call my onclick event if function call returns number > 0, something like: if(checkNoOfRows() > 0) //call onclickevent

Is it possible to do this inline ?

Here's my button onclick event, which I would like to call only when certain condition is fulfilled:

<button id="btnExportXLSPropsChild" onclick="location.href='@Url.Action("exportUserProp", "UserManagement", new { typeToExport = "CHILD" })'">Export</button>
Share Improve this question asked Oct 6, 2015 at 16:06 FrenkyBFrenkyB 7,22717 gold badges74 silver badges125 bronze badges 2
  • Do you want to do the check on the server or the client? – James Brierley Commented Oct 6, 2015 at 16:11
  • I want to do the check on the client. – FrenkyB Commented Oct 6, 2015 at 16:18
Add a ment  | 

3 Answers 3

Reset to default 3

You can do this check in razor, but the syntax is a bit messy. You need the <text> elements so that it doesn't try to parse your onclick as code on the server:

<div @if (true) { <text>onclick="test"</text>  }></div>

However, a better approach would probably be to add the url as an attribute and then bind an event in javascript with the check. Your template will then be something like:

<button id="btnExportXLSPropsChild" data-url="@Url.Action("exportUserProp", "UserManagement", new { typeToExport = "CHILD" })">Export</button>

And then in the javascript with jQuery:

$("#btnExportXLSPropsChild").on('click', function(){
    if(checkNumberOfRows() > 0){
        var url = $(this).data('url');
        window.location.href = url;
    }
}

You should remove the inline event and use an event handler instead. From the JavaScript event handler, you can check how many rows exist and conditionally run your function.

$("#btnExportXLSPropsChild").on('click', function(){
    if(checkNumberOfRows() > 0){
        //code goes here
    }
}

Jquery can do the magic for you :

$('button').click(function(){ if(condition > 0) { windown.location.href = '@Url.Action("Action","Controller")'; } });

Post a comment

comment list (0)

  1. No comments so far