簡體   English   中英

如何在javascript中將對象轉換為字符串?

[英]how to convert object to string in javascript?

下面我在這里粘貼了我的一段JavaScript代碼

1)

  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');

如何將其作為值檢索? 我不想檢索為對象

請幫助我。

只需要抓住價值

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

雖然您應該確保它存在

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

還是我誤會了您要檢索的內容?

您是否嘗試過:

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

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

嘗試這個

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

getElementById將返回該元素作為對象。 沒有辦法解決。

您需要做的就是從中獲得價值:

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

或者您可以一行完成:

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

是否:

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

返回表單類型string {input type="text" id="currentCustomer"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM