在cshtml文件中加载一个函数

时间:2018-04-05 16:09:00

标签: javascript jquery html razor partial-views

我正在开发一个包含4个导航标签的Web应用程序。

名为index.cshtml的主要cshtml文件包含4个nav-tabs作为部分视图:

var Parse = require('parse/react-native');

// function NSManaged(target, property, descriptor) {
function NSManaged<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) {
    return {
        set: function (value) {
            if ( target instanceof Parse.Object ) {
                target.set( propertyKey, value, {
                    error: function(obj, error) {
                        console.log( 'the set failed: ' + error);
                        }
                    });
                console.log('target: ' + target.constructor.name + ' property: ' + propertyKey + ' value: ' + value);
            } else {
                console.log( 'target is not an instanceof Parse.Object');
            }
        },
        get: function() {
            let val = target.get( propertyKey);
            console.log('get', val);
            return val;
        },
        enumerable: true,
        configurable: true
    };
  };

export class FooModel extends Parse.Object {
    @NSManaged
    name?: string

    constructor() {
        // @ts-ignore
        super('FooModel');
    }

}


let foo = new FooModel();
foo.name = "test";
foo.set( "name2", "test" );
foo.save();

Historie.cshtml文件有两个功能:

<div class="tab-content" id="mainTab" >
        <div class="tab-pane fade in active" id="@ViewData["Stamp"]" role="tabpanel" aria-labelledby="stempel-tab">
          @{ViewBag.Title = "Index";}
          @{Html.RenderAction("_Stempel", "Home");}
        </div>
        <div class="tab-pane fade" id="@ViewData["BackDated"]" role="tabpanel" aria-labelledby="backdated-tab">
          @{ViewBag.Title = "Index";}
           @{Html.RenderAction("_BackDated", "Home");}
        </div>
        <div class="tab-pane fade" id="@ViewData["History"]" role="tabpanel" aria-labelledby="historie-tab">
           @{ViewBag.Title = "Index";}
           @{Html.RenderAction("_Historie", "Home");}
        </div>
        <div class="tab-pane fade" id="@ViewData["ChangePin"]" role="tabpanel" aria-labelledby="changepin-tab">
           @{ViewBag.Title = "Index";}
           @{Html.RenderAction("_Pinaendern", "Home");}          
        </div>
    </div> 

如果我将函数放在document.ready中,每次WebApp执行函数时都需要更新。

ShowBookings();
CleanNoHistoryMessage();

Historie.cshtml

 $(document).ready(function () {
        ShowBookings();
        CleanNoHistoryMessage();

    });

我想在每次单击选项卡时执行这些功能。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

使用jQuery .on()事件处理程序附件:

  

将一个或多个事件的事件处理函数附加到所选元素。

点击sp,所以你的代码就像:

&#13;
&#13;
$("#mainTab .tab-pane").on("click", function() {
  console.log($(this).text() + "Is clicked");
  /*    ShowBookings();
        CleanNoHistoryMessage(); */
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="tab-content" id="mainTab">
  <div class="tab-pane fade in active"  role="tabpanel" aria-labelledby="stempel-tab">
    tab1
  </div>
  <div class="tab-pane fade"  role="tabpanel" aria-labelledby="backdated-tab">
    tab2
  </div>
  <div class="tab-pane fade"  role="tabpanel" aria-labelledby="historie-tab">
    tab3
  </div>
  <div class="tab-pane fade"  role="tabpanel" aria-labelledby="changepin-tab">
    tab4
  </div>
</div>
&#13;
&#13;
&#13;