开发者

how to convert object to string in javascript?

Below i have pasted my piece of javascript code here

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>

the above code is working fine. The thing is i need to retrieving开发者_StackOverflow value from "showCustomerName()" to "showCustomerID()" script .

In "showCustomerID()" script method i am retrieving "showCustomerName()" methods value as object

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

how to retrieve it as a value? i dont want to retrieve as object

please help me guys.


Just need to grab the value

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

Though you should make sure it exsts

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

Or am I misunderstanding what you are trying to retrieve?


Did you tried this :

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


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

Getting an element is an object. You can then target the object to get its value.


try this

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


getElementById is going to return the element as an object. There's no way around that.

What you need to do is get the value outof that:

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

or you could do it in one line:

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


Does:

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

return a string for a form type {input type="text" id="currentCustomer"}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜