0

我尝试从所选行中获取值并将它们作为参数传递给 $.getJSON。我可以得到这个值,但是,当我点击链接时,值之前和之后会出现奇怪的字符。链接中的字符显示为 %OA++++++++++值+++++++%0A。

这是我的代码

var className='';
var Section='';
$('#listsubject tr').click(function () {
            var th = $(this);
            var td = $(this).find('td');

            $.each(td, function (index, item) {
                if (index == 2) className = item.innerHTML;

            });


 $.getJSON('@Url.Action("getStudentList/","Student")',
                { classname: className
                }, function (data) {
   alert('test');
});

请帮助我。我被困在这里

提前致谢

编辑

当我尝试代码时

$.getJSON('@Url.Action("getStudentList/","Student")',
                    { classname: className,
                      section:'A'
                    }, function (data) {
       alert('test');
    });

在链接中,部分显示正常,唯一的问题是类名

更新

小提琴链接 http://jsfiddle.net/gordon/vzTDc/2/

4

1 回答 1

1

试试这个。我认为现在还可以。

var className = '',
    Section = '';

// you were trying with $('#listsubject tr'), but first tr has no td
// so click should bind with tr from 2nd
// so correct selector will be $('#listsubject tr:gt(0)')

$('#listsubject tr:gt(0)').click(function() {
    var th = $(this);
    var td = $(this).find('td');
    $.each(td, function(index, item) {

        // As you have 2 td with in each row, so the index will be 0 and 1
        // not 2 and 3

        if (index == 0) {
            className = $.trim($(item).text()); // $.trim() will remove spaces
        }
        if (index == 1) {
            Section = $.trim($(item).text());
        }
    });
    console.log('ClassName: ' + className + ', Section: ' + Section);
    $.getJSON('StudentMarks/getSubjectGrading', {
        classname: className,
        section: Section
    }, function(data) {
        alert(data);
    });
});

演示

于 2012-05-26T15:25:52.130 回答