JQuery在使用变量进行选择时非法调用

时间:2012-11-25 20:25:02

标签: javascript jquery

我的javascript似乎不喜欢var divTag = '#' + ratinglistid + " > select[name=SelectedValue] > option:selected";

我找不到搜索的答案..抱歉格式混乱,无法让它消失。

 Script:

<script>
    function doSubmit(form, myRating, ratinglistid){
        var divTag = '#' + ratinglistid + " > select[name=SelectedValue] > option:selected";
        var ratingValue = $(divTag);
        $.ajax({
            type: "GET",
            url: "/RatingLists/Done",
            data: ({
                myrating: ratingValue,
                myRatingListId: ratinglistid
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    }
</script>

Html我想从中获得价值:

    <div id='2'>
    <input type="hidden" name="RatingListId" value="2"/>
    <select id="SelectedValue" name="SelectedValue" onchange="doSubmit($(this).parents(&#39;form&#39;),2);"><option value="1">Inget intresse, tas bort</option>
    <option value="2">Svagt intresse, sparas</option>
    <option selected="selected" value="3">Intresserad-ingen kontakt &#228;n</option>
    <option value="4">4-Intreserad-vill ha kontakt</option>
    <option value="5">5-Vill byta!</option>
    <option value="100">6-Avvaktar v&#228;rdar</option>
    </select>
    </div

2 个答案:

答案 0 :(得分:0)

你试图在ajax函数中传递一个jQuery对象,导致Illegal invocation错误,使用.val()获取表单元素的值。

data: ({
    myrating: ratingValue.val(),
    myRatingListId: ratinglistid
}),

答案 1 :(得分:0)

它无法评估内部来电,所以我必须这样做:

var divTag = '#' + ratinglistid + " > select[name=SelectedValue] > option:selected";
var ratingValue = $(divTag).val();
    $.ajax({
                type: "GET",
                url: "/RatingLists/Done",
                data: ({
                    myrating: ratingValue,
                    RatingListId: ratinglistid
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess(ratinglistid),
                error: function () {
                    var id = ratinglistid;
                    $('#' + id + "tbody td").hover(function () {
                        $(this).parents('tr').addClass('highlight');
                    }, function () {
                        $(this).parents('tr').removeClass('highlight');
                    });
                }
            });