...但是如果该值最初为空,例如“”,则无法使用相同的键更改该值!
function loadDialog(link, e, ajaxRequest) {
e.preventDefault();
var $title = link.innerHTML;
var $contenturl = $(link).attr('href');
var $dialog = $('<div id="MyDialog"></div>');
// Read value from hidden input field
var templateIdValue = $('input[name=TemplateId]').val();
// Change the value: This will always and only happen the 2nd time I come here,
// the first time the templateIdValue will always be empty
if (templateIdValue != '') {
templateIdValue = 777;
}
$dialog.load($contenturl).data('templateIdKey', templateIdValue).dialog({
title: $title,
autoOpen: true,
modal: true,
buttons: {
"OK": function () {debugger;
ajaxRequest($(this), $('form', this));
},
"Cancel": function () {
$dialog.dialog("close");
}
}
});
}
当第二次 templateIdValue 更改为 777
我来到这段代码:
在这里,我再次检索该值,它应该是 777。但它不是。它是空的 !
@model ITMS.Web.Models.UnitViewModel
@*Remote Validation*@
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var templateId = $('#MyDialog').data('templateIdKey');
$('input[name=TemplateId]').val(templateId);
});
</script>
@using (Html.BeginForm("Create", "Unit"))
{
@Html.ValidationSummary(false)
<p class="editor-label">@Html.LabelFor(model => model.Name)</p>
<p class="editor-field">@Html.EditorFor(model => model.Name)</p>
<p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p>
@Html.HiddenFor(x => x.TemplateId)
}
如何更改第一次使用 .data() 方法传递的键的值?
更新
// Does not cache the ajax requests to the controller e.g. IE7/8/9 is doing that...
$.ajaxSetup({ cache: false });
$(document).ready(function () {
$('#OpenTemplate').click(function (event) { loadDialog(this, event, openTemplate); });
$('#CreateTemplate').click(function (event) { loadDialog(this, event, createTemplate); });
$('#DeleteTemplate').click(function (event) { loadDialog(this, event, deleteTemplate); });
$('#CreateUnit').click(function (event) { loadDialog(this, event, createUnit); });
$('#DeleteUnit').click(function (event) { deleteUnit(); });
$('#CreateTeststep').click(function (event) { loadDialog(this, event, createTeststep); });
$('#DeleteTeststep').click(function (event) { deleteTeststep(); });
$('#SaveTemplate').click(function (event) { saveTemplate(); });
});