原型到jQuery转换混乱

时间:2015-04-13 16:25:34

标签: javascript jquery prototypejs

我有以下功能:

function updateproductselectionxxx(form, productassignment, mainproductid, subprodqty) {
    var checkingurl = "shopajaxproductselection.asp";
    var pars = 'productassignment=' + productassignment + '&qty=' + subprodqty + '&mainid=' + mainproductid;
    var url = checkingurl + '?' + pars;
    var target = 'productselectionresult' + productassignment;
    var myAjax = new Ajax.Updater(target, checkingurl, {
        method: 'post',
        parameters: pars
    });
}

我目前正在将此网站上的所有javascript转换为jQuery。通常我可以使用类似的东西:

function updateproductselection(form, productassignment, mainproductid, subprodqty) {
    $.ajax({
        type: 'POST',
        url: 'shopajaxproductselection.asp',
        data: $(form).serialize(),
        success: function (response) {
            $(form).find('productselectionresult' + productassignment).html(response);
        }
    });

    return false;
}

这就是诀窍,但是我真的只想发送第一个函数中指示的1个字段,并且我还希望在调用它时将我发送的信息直接发送给函数。 JavaScript绝对不是我的专业领域,但通常我可以糊里糊涂,但这次我尝试的所有内容都会导致错误,而且我没有走得太远。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

看起来POST和GET之间有点混乱。虽然请求方法在较旧的Prototype版本中设置为POST,但是params通过CGI发送,CGI通常作为GET出现在服务器上。在没有看到服务器端代码的情况下说起来有点难,但你可以试试这个,这样jQuery版本更接近于模仿旧的Prototype版本:

function updateproductselection(form, productassignment, mainproductid, subprodqty) {
    var checkingurl = "shopajaxproductselection.asp";
    var pars = 'productassignment=' + productassignment + '&qty=' + subprodqty + '&mainid=' + mainproductid;
    var url = checkingurl + '?' + pars;
    $.ajax({
        type: 'POST',
        url: url,
        data: {},
        success: function (response) {
            $(form).find('#productselectionresult' + productassignment).html(response);
        }
    });

    return false;
}

请注意,我已在#的开头添加了哈希productselectionresult - 由于PrototypeJS的工作方式不同,这一点至关重要。在Prototype中,您可以使用ID选择器,如:

$('id')

而在jQuery中它必须是:

$('#id')