检查点击了两个按钮中的哪一个
Check which of the two buttons is clicked
我有一个页面有两个按钮 id=btn-config 和 id=btn-json-config 单击时显示两个不同文件的内容,并具有将更改保存到该文件的功能。
这两个文件的代码是重复的,所以我试图将它们合并到同一个 .cshtml 文件中。
<script>
$("#btn-save-code").click(function (e) {
var connectionId = sessionStorage.getItem("connectionId");
var filename = $("#modal-title").html();
var contents = codeMirrorEditor.getValue();
var repository = $("#repositories").val();
var branch = $("#branches").val();
var token = $("#__AjaxAntiForgeryForm input").val();
var dataWithToken = {
__RequestVerificationToken: token,
filename: filename,
connectionId: connectionId,
fileContents: contents,
repositoryName: repository,
branch: branch
};
$.ajax({
type: "POST",
data: dataWithToken,
url: '@Url.Action("SaveFileContents", "Home")',
url: '@Url.Action("SaveStepFileContents", "Home")',
success: function (data) {
$("#modal-code-editor").modal('hide');
$.growl.notice({ message: "File saved successfully" });
},
error: function () {
$.growl.error({ message: "Saving file failed" });
}
});
});
</script>
目前,当我按下保存按钮时,我得到了 "file saved successfully" 和 "Saving file failed"。
我想做的是检查按下了两个按钮中的哪一个,以避免重复消息。
我试过了
$("#btn-config,#btn-json-config").click(function () {
if (this.id == "btn-config") {
var url = '@Url.Action("SaveFileContents", "Home")';
return url;
}
else if (this.id == "btn-json-config") {
var url = '@Url.Action("SaveStepFileContents", "Home")';
return url;
}
});
和
$('#btn-config,#btn-json-config').click(function () {
var t = $(this).attr('id');
});
if ($(t == "btn-config")) {
url: '@Url.Action("SaveFileContents", "Home")'
} else if ($(t == "btn-json-config")) {
url: '@Url.Action("SaveStepFileContents", "Home")'
};
但我不知道如何继续。
你这里有很多一厢情愿的想法。
这个 $(t == "btn-config")
甚至无效 jQuery 并且也没有试图设置 url 两次
您需要这样做:
var t;
$('#btn-config,#btn-json-config').click(function () {
t = $(this).attr('id');
});
然后
url: t == "btn-config" ? '@Url.Action("SaveFileContents", "Home")' : '@Url.Action("SaveStepFileContents", "Home")'
既然你没有post你的HTML,以上就是我能做的最好的了。不过如果是收音机之类的,还有更好的办法
我有一个页面有两个按钮 id=btn-config 和 id=btn-json-config 单击时显示两个不同文件的内容,并具有将更改保存到该文件的功能。
这两个文件的代码是重复的,所以我试图将它们合并到同一个 .cshtml 文件中。
<script>
$("#btn-save-code").click(function (e) {
var connectionId = sessionStorage.getItem("connectionId");
var filename = $("#modal-title").html();
var contents = codeMirrorEditor.getValue();
var repository = $("#repositories").val();
var branch = $("#branches").val();
var token = $("#__AjaxAntiForgeryForm input").val();
var dataWithToken = {
__RequestVerificationToken: token,
filename: filename,
connectionId: connectionId,
fileContents: contents,
repositoryName: repository,
branch: branch
};
$.ajax({
type: "POST",
data: dataWithToken,
url: '@Url.Action("SaveFileContents", "Home")',
url: '@Url.Action("SaveStepFileContents", "Home")',
success: function (data) {
$("#modal-code-editor").modal('hide');
$.growl.notice({ message: "File saved successfully" });
},
error: function () {
$.growl.error({ message: "Saving file failed" });
}
});
});
</script>
目前,当我按下保存按钮时,我得到了 "file saved successfully" 和 "Saving file failed"。 我想做的是检查按下了两个按钮中的哪一个,以避免重复消息。
我试过了
$("#btn-config,#btn-json-config").click(function () {
if (this.id == "btn-config") {
var url = '@Url.Action("SaveFileContents", "Home")';
return url;
}
else if (this.id == "btn-json-config") {
var url = '@Url.Action("SaveStepFileContents", "Home")';
return url;
}
});
和
$('#btn-config,#btn-json-config').click(function () {
var t = $(this).attr('id');
});
if ($(t == "btn-config")) {
url: '@Url.Action("SaveFileContents", "Home")'
} else if ($(t == "btn-json-config")) {
url: '@Url.Action("SaveStepFileContents", "Home")'
};
但我不知道如何继续。
你这里有很多一厢情愿的想法。
这个 $(t == "btn-config")
甚至无效 jQuery 并且也没有试图设置 url 两次
您需要这样做:
var t;
$('#btn-config,#btn-json-config').click(function () {
t = $(this).attr('id');
});
然后
url: t == "btn-config" ? '@Url.Action("SaveFileContents", "Home")' : '@Url.Action("SaveStepFileContents", "Home")'
既然你没有post你的HTML,以上就是我能做的最好的了。不过如果是收音机之类的,还有更好的办法