简体   繁体   English

如何在MVC asp.net中使用jquery c#

[英]How to use jquery in MVC asp.net c#

Currently I am working on online application form using MVC asp.net . 目前我正在使用MVC asp.net开发在线申请表。

This is my form. 这是我的表格。

在此输入图像描述

What I want is that when user choose Individual radiobutton, the other radionbutton textfield should be disabled. 我想要的是当用户选择Individual radiobutton时,应该禁用另一个radionbutton文本字段。 I managed to achieved this using JSFiddle here . 我成功地实现了这一点使用的jsfiddle 这里

What I did was 我做的是
1) Copied the coding from JSFiddle that I have created into the MVC view 1)将我创建的JSFiddle的编码复制到MVC视图中

@model Insurance.ViewModels.RegisterInfoPA
@{
  ViewBag.Title = "Insert";
 }
 <h2>Insert Fire Insurance</h2>

<script>
$(".radioBtn").click(function () {
    $("#reg_title").attr("disabled", true);
    $("#reg_registerNm").attr("disabled", true); //Comp_Name//Name
    $("#reg_identityNo").attr("disabled", true); //Ic_No//army//com_regis
    $("#pinfo_gender").attr("disabled", true);
    $("#busInfo_dateRegisCompany").attr("disabled", true);
    $("#reg_dateCompRegis").attr("disabled", true); //DOB
    $("#pinfo_occupation").attr("disabled", true);
    $("#pinfo_maritalId").attr("disabled", true);
    $("#busInfo_contactNm").attr("disabled", true);
    $("#busInfo_natureBusiness").attr("disabled", true);

    if ($("input[name=reg.identityId]:checked").val() == "1") {
        $("#reg_title").attr("disabled", false);
        $("#reg_identityNo").attr("disabled", false);
        $("#pinfo_gender").attr("disabled", false);
        $("#reg_dateCompRegis").attr("disabled", false);
        $("#pinfo_maritalId").attr("disabled", false);
        $("#pinfo_occupation").attr("disabled", false);
    }
    if ($("input[name=reg.identityId]:checked").val() == "2") {
        $("#reg_registerNm").attr("disabled", false);
        $("#busInfo_dateRegisCompany").attr("disabled", false);
        $("#busInfo_natureBusiness").attr("disabled", false);
        $("#busInfo_contactNm").attr("disabled", false);
    }
});
   </script>
    @using (Html.BeginForm())
   {       
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<link href="~/Content/FlexiPA.css" rel="stylesheet" />
<fieldset>
    <legend>register</legend>
    <div class="flexiPAtitle">
        <h3>
            <b>
                &nbsp;&nbsp;
                @Html.RadioButtonFor(model => model.reg.identityId, 1, new { @class = "radioBtn" })
                Individual Applicant&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                    
                @Html.RadioButtonFor(model => model.reg.identityId, 2, new { @class = "radioBtn" })
                Company Application

                @Html.HiddenFor(model=>model.reg.insuranceTypeId, 3)
            </b>
        </h3>
    </div>

I placed the code before @Html.BeginForm() . 我把代码放在@ Html.BeginForm()之前。 But it is not working. 但它没有用。

2) So I tried to place the code into different js file then call it from view 2)所以我试着将代码放入不同的js文件,然后从视图中调用它

<h2>Insert Fire Insurance</h2>
  <script src="~/Scripts/IndividualCompany.js"></script>
  @using (Html.BeginForm())
  {   

It is still not working. 它仍然无法正常工作。 Any ideas where I did wrong. 我做错了什么想法。 How to use this jquery code in MVC, it confuses me, because when I did the code in JSFiddle everything is ok, but not in MVC. 如何在MVC中使用这个jquery代码,它让我感到困惑,因为当我在JSFiddle中编写代码时,一切正常,但不是在MVC中。 I have also install Jquery packages from the nuget manager. 我还从nuget管理器安装了Jquery包。 Really need some guidance. 真的需要一些指导。

Thank you. 谢谢。

Copy and paste the code below exactly as it is below and it will work.I tested on my side and it works just fine. 复制并粘贴下面的代码完全如下所示,它将工作。我在我身边测试,它工作得很好。

@model Insurance.ViewModels.RegisterInfoPA

@{
    ViewBag.Title = "Insert";
}

<h2>Insert Fire Insurance</h2>

<script src="//code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {

        alert('jQuery Load.If this message box shows it means jQuery is correctly referenced and can be used on this page');

        $(".radioBtn").click(function () {

            alert('radioBtn click.If this message box shows the radio button click event is working');

            $("#reg_title").attr("disabled", true);
            $("#reg_registerNm").attr("disabled", true); //Comp_Name//Name
            $("#reg_identityNo").attr("disabled", true); //Ic_No//army//com_regis
            $("#pinfo_gender").attr("disabled", true);
            $("#busInfo_dateRegisCompany").attr("disabled", true);
            $("#reg_dateCompRegis").attr("disabled", true); //DOB
            $("#pinfo_occupation").attr("disabled", true);
            $("#pinfo_maritalId").attr("disabled", true);
            $("#busInfo_contactNm").attr("disabled", true);
            $("#busInfo_natureBusiness").attr("disabled", true);

            if ($("input[name=reg.identityId]:checked").val() == "1") {
                $("#reg_title").attr("disabled", false);
                $("#reg_identityNo").attr("disabled", false);
                $("#pinfo_gender").attr("disabled", false);
                $("#reg_dateCompRegis").attr("disabled", false);
                $("#pinfo_maritalId").attr("disabled", false);
                $("#pinfo_occupation").attr("disabled", false);
            }
            if ($("input[name=reg.identityId]:checked").val() == "2") {
                $("#reg_registerNm").attr("disabled", false);
                $("#busInfo_dateRegisCompany").attr("disabled", false);
                $("#busInfo_natureBusiness").attr("disabled", false);
                $("#busInfo_contactNm").attr("disabled", false);
            }
        });
    });
</script>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <link href="~/Content/FlexiPA.css" rel="stylesheet" />
    <fieldset>
        <legend>register</legend>
        <div class="flexiPAtitle">
            <h3>
                <b>
                    &nbsp;&nbsp;
                    @Html.RadioButtonFor(model => model.reg.identityId, 1, new { @class = "radioBtn" })
                    Individual Applicant&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.RadioButtonFor(model => model.reg.identityId, 2, new { @class = "radioBtn" })
                    Company Application

                    @Html.HiddenFor(model => model.reg.insuranceTypeId, 3)
                </b>
                <input id="reg_title" type="text" />
            </h3>
        </div>
    </fieldset>
}

As the comment says, the DOM elements you are trying to attach to don't exist when the script is executed. 正如评论所说,执行脚本时,您尝试附加的DOM元素不存在。

You either need to put the in-line script from your first at the bottom of the template (after the form elements) so that the DOM elements are loaded, or you need to wrap it in a page ready function, which is probably what jsfiddle does. 您需要将第一个内嵌脚本放在模板底部(在表单元素之后)以便加载DOM元素,或者您需要将其包装在页面就绪函数中,这可能就是jsfiddle确实。

In the latter case you can wrap all your script code in something like this: 在后一种情况下,您可以将所有脚本代码包装在以下内容中:

$(document).ready(function(){
    $(".radioBtn").click(function (){...
});

As the accepted answer says, you also need to make sure jquery is loaded on the page. 正如接受的答案所说,您还需要确保在页面上加载了jquery。

缺少JQuery Lib文件,只需添加JQuery Lib File顶部javascript即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM