Javascript& JQuery奇怪的问题或我做错了什么?

时间:2011-12-05 18:28:50

标签: javascript jquery

我有这个函数用于处理从一个窗口到另一个窗口的值,而不是另一个...

工作脚本:

$(document).ready(function(e) {
    $('#clickit').live({
        click: function() { 
            window.opener.document.forms['orderForm']['service'].value = document.forms['GroundRates']['service'].value;
            window.opener.document.forms['orderForm']['rate'].value = document.forms['GroundRates']['rate'].value;
            self.close();
            return false;
        }
    });
});

现在我在另一个剧本上,我做错了什么?我把头发拉出来了。

不工作:

$(document).ready(function(e) {
    $('#clickit').live({
        click: function() { 
            var thisservice = document.forms['GroundRates']['service'].value;
            var thisrate = document.forms['GroundRates']['rate'].value;
            var thatservice = window.opener.document.forms['orderForm']['service'].value;
            var thatrate = window.opener.document.forms['orderForm']['rate'].value;
            $(thatrate) = $(thisrate);
            $(thatservice) = $(thisservice);
            self.close();
            return false;
        }
    });
});

我也试过..

$(thatrate).val() = $(thisrate).val();
$(thatservice).val() = $(thisservice).val();

和..

thatrate = thisrate;
thatservice = thisservice;

但这有效:

var service = document.forms['GroundRates']['service'].value;
var rate = document.forms['GroundRates']['rate'].value;
window.opener.document.forms['orderForm']['service'].value = service;
window.opener.document.forms['orderForm']['rate'].value = rate;

我没有正确地为window.opener分配var吗?

4 个答案:

答案 0 :(得分:2)

你误用了.val()

$(thatrate).val($(thisrate).val());
$(thatservice).val($(thisservice).val());

新值在括号内。

答案 1 :(得分:1)

尝试:

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];
thatrate.val(thisrate.val());
thatservice.val(thisservice.val());

答案 2 :(得分:1)

您的控制台会告诉您:ReferenceError: Invalid left-hand side in assignment

$(thatrate) = $(thisrate);
$(thatservice) = $(thisservice);

你应该这样做:

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];

thatrate.value = thisrate.value
thatservice.value = thisservice.value;

答案 3 :(得分:1)

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];

thatrate.value = thatservice.value;
thatservice.value = thisservice.value;

或者如果你想用jQuery对象包装DOM对象。

var thisservice = document.forms['GroundRates']['service'];
var thisrate = document.forms['GroundRates']['rate'];
var thatservice = window.opener.document.forms['orderForm']['service'];
var thatrate = window.opener.document.forms['orderForm']['rate'];

$(thatrate).val( $(thatservice).val() );
$(thatservice).val( $(thisservice).val() );