Javascript 无法从html检索数据
至于我下面的代码,我无法从tr获取sectionID,我需要在每次单击delete按钮时获取sectionID的动态id,但它总是给我空值 以下是Jquery脚本:Javascript 无法从html检索数据,javascript,c#,jquery,ajax,asp.net-mvc,Javascript,C#,Jquery,Ajax,Asp.net Mvc,至于我下面的代码,我无法从tr获取sectionID,我需要在每次单击delete按钮时获取sectionID的动态id,但它总是给我空值 以下是Jquery脚本: <script> $(function () { $('.btnDelete').click(function () { var sectionID = $(this).closest('tr').find('.sectionID'); $.ajax({
<script>
$(function () {
$('.btnDelete').click(function () {
var sectionID = $(this).closest('tr').find('.sectionID');
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: 'CheckSectionIDagainststudentID',
data: JSON.stringify({ sectionID: sectionID.html() }),
success: function (data) {
if (data == true) {
$.alert("Cannot delete | Students exsisting in this
Section!");
}
else if (data == false) {
var secCode = JSON.parse(this.data);
var code = sectionid['sectionid'];
window.location.href = "SectionDelete?Page=data§ionid="
+ code;
}
},
failure: function (response) {
$('#result').html(response);
}
});
});
});
</script>
使用字符串数组:
[HttpPost]
public ActionResult CheckSectionIDagainststudentID(string[] sectionID)
{
return Json(sectionID);
}
使用字符串数组:
[HttpPost]
public ActionResult CheckSectionIDagainststudentID(string[] sectionID)
{
return Json(sectionID);
}
根据您的问题,您无法从var
sectionID=$(this.nexist('tr').find('.sectionID'))中获取值代码>
因此,这里有一个方法,你可以实现你的结果
//Your Dynamic Button should look like this
//in value Bind your sectionID @ MVC
<button class="btnDelete" value="5" onclick="AjaxDelete(this)">Delete</button>
//For Function
function AjaxDelete(values) {
//Write this in Ajax
//Fetch Value from attribute value
var sectionID = $(values).attr("value"); // you will get 5 as value
//Your Ajax Call with SectionId as Parameter
}
根据您的问题,您无法从varsectionID=$(this.nexist('tr').find('.sectionID'))中获取值代码>
因此,这里有一个方法,你可以实现你的结果
//Your Dynamic Button should look like this
//in value Bind your sectionID @ MVC
<button class="btnDelete" value="5" onclick="AjaxDelete(this)">Delete</button>
//For Function
function AjaxDelete(values) {
//Write this in Ajax
//Fetch Value from attribute value
var sectionID = $(values).attr("value"); // you will get 5 as value
//Your Ajax Call with SectionId as Parameter
}
我想在这里做的是使用ajax从表中获取节id,然后将其与student表中sectionid的FK进行比较,如果它们匹配,则会给出一个错误,如果它们不匹配,则会将其重定向到controller中的delete方法。您在sectionid中获取值吗?字符串中的at函数?试试这个??var代码=sectionID['sectionID'];不,它在debugger中显示sectionID:null这不起作用,甚至无法将调试器指向控制器。data:JSON.stringify({sectionID:sectionID.html()}),即使这里它说空,我想在这里做的是使用ajax从表中获取节id,然后将其与学生表中的节id的FK进行比较,如果它们匹配,则会给出一个错误,如果它们不匹配,则会将其重定向到控制器中的delete方法。您在sectionID中获取值吗?字符串中的at函数?试试这个??var代码=sectionID['sectionID'];不,它在debugger中显示sectionID:null这不起作用,甚至无法将调试器指向控制器。data:JSON.stringify({sectionID:sectionID.html()}),即使在这里它也会说null为什么我只需要从该行获取id时需要字符串数组,而且我也尝试过同样的方法,或者为什么我只需要从该行获取id时需要字符串数组,另外,我也尝试过同样的错误,因为这个更改按钮甚至不起作用或重定向到控制器方法,我在这里添加了断点,它也不起作用,我通过将类添加到而不是来修复它,但现在我得到的答案是\n 1\n(1是id)。。。感谢您的帮助此更改按钮甚至不起作用或将其重定向到控制器方法,我在此处添加了断点也不起作用我通过将类添加到而不是来修复它,但现在我将得到我的答案为\n 1\n(1是id)。。。谢谢你的帮助
if ((~string.indexOf('\n '))) {
string= string.split('\n ')[1].split('\n')[0];
}