2010-08-24 40 views
1

下面我粘貼我的片這裏如何將對象轉換爲JavaScript中的字符串?

1 JavaScript代碼)

function showCustomerName(dropdown) { 
    var selectedCustomer = dropdown.options[dropdown.selectedIndex].value; 
    var currentCustomer = document.getElementById('currentCustomer'); 
    alert(selectedCustomer); 

    var context = document.forms[0].context.value; 
    document.forms[0].mode.value = "UPDATE"; 
    document.forms[0].custName.value = selectedCustomer ; 
    document.forms[0].action=context+"/updateReportDetail.do"; 
    document.forms[0].method="POST"; 
    document.forms[0].submit(); 
} 

2)

function showCustomerID(dropdown) { 
    var currentCustomer = document.getElementById('currentCustomer'); 
    var selectedCustomerID = dropdown.options[dropdown.selectedIndex].value; 
    alert(selectedCustomerID); 

    var context = document.forms[0].context.value; 
    document.forms[0].mode.value = "UPDATE"; 
    document.forms[0].custName.value = currentCustomer; 
    document.forms[0].custID.value = selectedCustomerID ; 
    document.forms[0].action=context+"/updateReportDetail.do"; 
    document.forms[0].method="POST"; 
    document.forms[0].submit(); 
} 


<select id="currentCustomer" onchange="showCustomerName(this)"> 
<c:forEach var="Customer" items="${listCustomer}" > 
    <option value="<c:out value="${Customer}" />"><c:out value="${Customer}" /> 
    </option> 
    </c:forEach> 
</select> 

<select id="currentCustomerID" onchange="showCustomerID(this)"> 
<c:forEach var="CustomerID" items="${listCustomerID}" > 
    <option value="<c:out value="${CustomerID}" />"><c:out value="${CustomerID}" /> 
    </option> 
    </c:forEach> 
</select> 

上面的代碼工作正常。 事情是我需要檢索值從「showCustomerName()」到「showCustomerID()」腳本。

在 「showCustomerID()」 腳本方法,我檢索 「showCustomerName()」 方法的值對象

var currentCustomer = document.getElementById('currentCustomer'); 

如何找回它作爲一種價值?我不想檢索作爲對象

請幫助我的傢伙。

+0

你不能調用currentCustomer.toString()嗎? – Holystream 2010-08-24 16:48:58

回答

4

只需要搶值

var currentCustomer = document.getElementById('currentCustomer').value; 
alert(currentCustomer); 

雖然你應該確保它exsts

var currentCustomerElement = document.getElementById('currentCustomer'); 
var currentCustomer = (currentCustomerElement) ? currentCustomerElement.value : "not found"; 

還是我誤解你是什麼試圖檢索?

2

你有沒有試過這樣:

var currentCustomer = document.getElementById('currentCustomer').value; 
2
var currentCustomer = document.getElementById('currentCustomer').value; 

獲取元素是一個對象。然後,您可以將對象定位到獲取其值。

1

試試這個

var currentCustomer = document.getElementById('currentCustomer').value; 
1

getElementById將會元素作爲對象返回。這是沒有辦法的。

你需要做的是讓outof該值:

var currentCustomerEle = document.getElementById('currentCustomer'); 
var currentCustomer = currentCustomerEle.value; 

,或者你能做到這一點的一條線:

var currentCustomer = document.getElementById('currentCustomer').value; 
0

請問:

var currentCustomer = document.getElementById('currentCustomer').value; 

回報string for a 表格類型{input type="text" id="currentCustomer"}

+0

你的回答並沒有真正增加任何東西給已經提出的問題。 – faester 2017-09-08 13:03:42

相關問題