视图

@Html.DropDownList("DeptId", null, "-- Select --", htmlAttributes: new { @class = "form-control", @onchange = "changeDept(this)" })


Javascript功能

function changeDept(ddl) {
    var lastValue = $(ddl).data('lastValue');
    var newValue = $(ddl).val
    var t = $(ddl + ' option:selected').text();

    if (confirm("Are you sure you want to change from " + $(this).data('lastText') + " to " + ($(this) + 'option:selected').text() + "?")) {
        $(this).data('lastValue', newValue);
    }
    else {
        $(this).val(lastValue);
    }
};

function selectDept(ddl) {
    $(this).data('lastText', $($(this) + ' option:selected').text());
    $(this).data('lastValue', $(this).val());
};


原始功能:

dept.change(function (e) {
    var lastValue = $(this).data('lastValue');
    var newValue = $(this).val();

    if (confirm("Are you sure you want to change from " + $(this).data('lastText') + " to " + $('#DeptId option:selected').text() + "?")) {
        $(this).data('lastValue', newValue);
    }
    else {
        $(this).val(lastValue);
    }
});
dept.each(function () {
    //Store old value
    $(this).data('lastText', $('#DeptId option:selected').text());
    $(this).data('lastValue', $(this).val());
});


第二种方法(原始函数)有效,但是我想使我的函数通用,换句话说,我想至少在5个视图中使用它,因此我不需要为每个通过选择器参数的视图都使用5个函数。如何配置第一个表格(第一个功能)使其起作用?

其实:

@Html.DropDownList("DeptId", null, "-- Select --", htmlAttributes: new { @class = "form-control", @onchange = "onChange(this)", @onblur = "onBlur(this)" })

function onChange(ddl) {
    var $ddl = $(ddl);
    var lastText = $ddl.data('lastText');
    var lastValue = $ddl.data('lastValue');
    var newText = $ddl.find(':selected').text();
    var newValue = $ddl.val();
    var bool = false;

    if (lastText === undefined) {
        bool = true;
    }

    else if (!confirm("Are you sure you want to change from " + lastText + " to " + newText + "?")) {
        bool = true;
    }

    else {
        bool = false;
        $ddl.val(lastValue);
    }

    if (bool) {
        $ddl.data('lastValue', newValue);
        $ddl.data('lastText', newText);
    }
};
function onBlur(ddl) {
    var selectedOption = ddl.options[ddl.selectedIndex];
    $(ddl).data('lastText', selectedOption.text).data('lastValue', selectedOption.value);
};


我的选择:

<div class="col-md-10">
   <select class="form-control" id="DeptId" name="DeptId" onblur="onBlur(this)" onchange="onChange(this)">
       <option value="">-- Select --</option>
       <option value="1">Clothing</option>
       <option value="2">Eletronics</option>
       <option value="3">Housewares</option>
       <option value="4">Technology</option>
       <option value="5">Accessories</option>
       <option value="6">Perfumary</option>
    </select>
</div>

最佳答案

当您使用内联html处理程序时,它们在全局上下文中调用,因此函数内部的this引用全局对象,在本例中为window。因此,代替this关键字,您需要使用传递的参数



function OnChange(ddl) {
  var $ddl = $(ddl);
  var lastValue = $ddl.data('lastValue');
  var newText = $ddl.find(':selected').text();
  var newValue = $ddl.val();

  if (confirm("Are you sure you want to change from " + $ddl.data('lastText') + " to " + newText + "?")) {
    $ddl.data('lastValue', newValue);
    $ddl.data('lastText', newText);
  } else {
    $ddl.val(lastValue);
  }

}

function onBlur(ddl) {
  var selectedOption = ddl.options[ddl.selectedIndex];
  $(ddl).data('lastText', selectedOption.text).data('lastValue', selectedOption.value);

};

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select onchange="OnChange(this)" onblur="onBlur(this)">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
</select>





禁用更新



function onChange(ddl) {
  var $ddl = $(ddl);

  var lastText = $ddl.data('lastText'),
    newText = $ddl.find(':selected').text();

  if (!lastText || confirm("Are you sure you want to change from " + lastText + " to " + newText + "?")) {
    $ddl.data('lastValue', $ddl.val());
    $ddl.data('lastText', newText);

  } else {
    $ddl.val($ddl.data('lastValue'));
  }


}

function onBlur(ddl) {
  var selectedOption = ddl.options[ddl.selectedIndex];
  $(ddl).data('lastText', selectedOption.text).data('lastValue', selectedOption.value);

};

$('select [value=""]').attr("disabled", "disabled");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control" id="DeptId" name="DeptId" onblur="onBlur(this)" onchange="onChange(this)">
  <option value="">-- Select --</option>
  <option value="1">Clothing</option>
  <option value="2">Eletronics</option>
  <option value="3">Housewares</option>
  <option value="4">Technology</option>
  <option value="5">Accessories</option>
  <option value="6">Perfumary</option>
</select>





UPDATE2:似乎不需要模糊处理程序,因为在某些浏览器(如IE)中,当确认显示时请选择失去焦点并提高模糊处理程序,并保存已选择的值,然后再进行确认。



function onChange(ddl) {
  var $ddl = $(ddl);

  var lastText = $ddl.data('lastText'),
    newText = $ddl.find(':selected').text();

  if (!lastText || confirm("Are you sure you want to change from " + lastText + " to " + newText + "?")) {
    $ddl.data('lastValue', $ddl.val());
    $ddl.data('lastText', newText);

  } else {
    $ddl.val($ddl.data('lastValue'));
  }


}

$('select [value=""]').attr("disabled", "disabled");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control" id="DeptId" name="DeptId" onchange="onChange(this)">
  <option value="">-- Select --</option>
  <option value="1">Clothing</option>
  <option value="2">Eletronics</option>
  <option value="3">Housewares</option>
  <option value="4">Technology</option>
  <option value="5">Accessories</option>
  <option value="6">Perfumary</option>
</select>

09-04 20:00