0

我正在尝试对复选框检查进行 ajax 调用。

这是我的代码:

 <%@ Control Language="C#" 
Inherits="System.Web.Mvc.ViewUserControl<EmsAdmin.Models.User>" %>

<%@ Import Namespace="EmsAdmin.Models" %>
<script type="text/javascript">
//<![CDATA[
alert("I am here");
var manuId = ('#value');
alert(manuId);

$('#value:checkbox').change(function () {
    var taskInput;
    alert("I am here" +'#<%: Model.ManufacturerId %>');
    $.ajax({
        url: "/ReportUserVerification/GetCountriesForManufacturer/<%:Model.ManufacturerId%>",

        type:'get',
        success: function (data) {
            taskInput = data;
            var countriesArray = [];
            for (var i = 0; i < data.length; i++) {
                var item1 = data[i];
                alert(item1);
                if (item1 != null) {
                    countriesArray.push(item1);
                }
            }
        }
    });
});

//]]>

<script type="text/javascript">
//<![CDATA[
alert("I am here");
var manuId = ('#value');
alert(manuId);

  $('#value').change(function () {
    var taskInput;
    alert("I am here" +'#<%: Model.ManufacturerId %>');
    $.ajax({
        url:
    "/ReportUserVerification/GetCountriesForManufacturer/<%:Model.ManufacturerId%>",

        type:'get',
        success: function (data) {
            taskInput = data;
            var countriesArray = [];
            for (var i = 0; i < data.length; i++) {
                var item1 = data[i];
                alert(item1);
                if (item1 != null) {
                    countriesArray.push(item1);
                }
            }
        }
    });
});

//]]>
</script>

<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try 
 again.") %>
<% using (Html.BeginForm())
 {%>
 <%= Html.AntiForgeryToken() %>
<p>
<%= Html.LabelFor(e=>e.Id,"Id:") %>
<%: Model.Id %>
</p>
<p>
<%= Html.LabelFor(e=>e.PersonId,"Person:") %>
<%= Html.DropDownListFor(e => e.PersonId, (SelectList)ViewData["allPersons"], "Select 
 person", new { @style = "width: 255px;" })%>
<%= Html.ValidationMessageFor(e=>e.Person,"") %>
</p>
 <p>
<%= Html.LabelFor(e=>e.Email,"Email:") %>
<%= Html.TextBoxFor(e => e.Email, new { @style = "width:250px;" })%>
<%= Html.ValidationMessageFor(e=>e.Email,"") %>
</p>
<p>
<%= Html.LabelFor(e=>e.Corporation.Description,"Corporation:") %>
<%= Html.TextBoxFor(e => e.Corporation.Description, new { @style = "width:250px;" })%>
<%= Html.ValidationMessageFor(e=>e.Corporation.Description,"") %>
</p>
<hr />
<h2>
 Approve User</h2>
 <p>
 <%= Html.LabelFor(e=>e.Approve,"Aprrove User:") %>
  <%= Html.CheckBoxFor(e=> e.Approve,Model.Approve) %><span>&nbsp;Switch this on if you 
 want to
    add regitrar </span>
<%= Html.ValidationMessageFor(e=>e.Approve,"") %>
</p>


如果需要,多选品牌:

  <div id ="ManufacturerId"<%:Model.ManufacturerDescription %>>
  <p>
     <%= Html.LabelFor(e=> e.ManufacturerDescription,"Brand:") %>  
    <br/>
 </p>

  <%for (int i = 0; i < Model.Manufacturers.Count;i++ )
  {%>
  <p>
                 <%:Html.HiddenFor(e=>e.Manufacturers[i].Id) %>
        <%:Html.CheckBoxFor(x => x.Manufacturers[i].IsSelected) %>
         <%:Html.LabelFor(x => x.CountryNames[i].IsSelected, 
    Model.Manufacturers[i].Description)%>
          </p>

  <%} %>
  </div>
  <%--<p>
  <%= Html.LabelFor(e=>e.ManufacturerDescription,"Brand:") %>
  <%= Html.DropDownListFor(e => e.ManufacturerDescription, 
  (SelectList)ViewData["allManufacturers"], "Select  Brands", new { @style = "width:

    255px;" })%>
   <%= Html.ValidationMessageFor(e=>e.ManufacturerDescription,"") %>

</p>


<%= Html.LabelFor(e=>e.CountryName,"Country:") %>
<%= Html.DropDownListFor(e => e.CountryName, (SelectList)ViewData["allCountries"], 
"Select Countries", new { @style = "width: 255px;" })%>
<%= Html.ValidationMessageFor(e=>e.CountryName,"") %>
</p>--%>

<hr />
 <h2>
 Select Role:</h2>
 <p>
 <%= Html.LabelFor(e => e.Role, "Role:")%>
 <%= Html.DropDownListFor(e => e.Role, (SelectList)ViewData["allRoles"], "Select 
  Roles",
 new { @style = "width: 255px;" })%>
<%= Html.ValidationMessageFor(e=>e.Role,"") %>
</p>
<p>
<input type="submit" value="Save" />
<%= Html.ActionLink("Cancel","Details",new {id=Model.Id},new {@title="exit without 
 saving"}) %>
 </p>
 <% } %>
4

1 回答 1

0

首先,在循环的每次迭代中,for您都div使用相同的 id 生成。在 HTML 中,在页面上有多个具有相同 id 的元素是非法的,并且在这种情况下,javascript 的行为可能真的是不可预测的。

其次,$('#value')只选择带有 的元素id="value",即 div。change如果某些内容发生更改,div 不会引发事件。要处理复选框的选择更改,您需要调整选择器以查询复选框本身。例如,像这样:

$('#value :checkbox').change(function () {
于 2013-07-31T11:22:18.063 回答