2011-12-05 61 views
0

我有這個功能對於應對從窗口窗口工作的一種方式的價值觀,而不是另一個...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?

+0

僅供參考,語法是'$(thatrate).VAL($(thisrate).VAL())' – climbage

回答

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()); 
+0

我明白我做錯了什麼。謝謝你的幫助! – Monty

2

你濫用.val()

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

新的價值遠遠括號內。

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()); 
+1

記住在你的例子'thatrate'不是一個jQuery對象,但原生DOM對象等等'。 val'不起作用。用戶'.value'代替。 –

+0

啊,是的,你說得對。 – DrXCheng

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; 
相關問題