初始化后如何更改文本区域的值?

时间:2018-12-24 23:32:01

标签: javascript html arrays forms

下面有一个我在JSBIN中创建的案例,所以我在文本区域中填充了数据,所以一旦我有了虚拟数据,我想更改memberid和其余数据,以便可以将请求发送给具有真实数据的后端,但是当我单击“提交”,它始终获取第一次在onload初始化的数据,任何想法如何在sumbit上更改数据时解决此问题,都应该具有新的数据,例如我在文本区域中键入的memberid。

main.html

var initialParams = [{
  "memberid": "etyhdjhsYYhajdsjdsQ==",
  "prescriptionInfo": [{
    "dispensingStoreDetails": {
      "dispensingStoreNumber": "1",
      "dispensingStoreNPINumber": "12302"
    }
  }]
}]

function init() {
  document.getElementById("spltyRxDetails").value = JSON.stringify(initialParams);
}
var specialtyRequest = {
  "lineOfBusiness": "SPECIALTY",
  "rxOrder": {
    "prescriptionDetails": []
  }
}
var requestParam = {
  orders: []
};
specialtyRequest.rxOrder.prescriptionDetails = initialParams;
requestParam.orders = specialtyRequest;
window.onload = init;

function execWrapperApi() {
  console.log(requestParam.orders);
}
<form>
  <div class="form-group memberInfo">
    <label for="client_id">Client Id</label>
    <input type="text" class="form-control" id="client_id" placeholder="Enter a Client Id">
  </div>
  <div class="form-group">
    <div class="spltyParams" style="border:1px solid rgb(242, 255, 0); width:100px; length:100px">
      <label for="spltyRxDetails">validate Order - Request Parameters</label>

      <textarea type="text" class="form-control text-area-request" id="spltyRxDetails" placeholder="Enter Validate Order - Request Parameters in JSON"></textarea>
    </div>
    <button type="button" class="btn btn-primary" onclick="execWrapperApi()">Submit</button> &nbsp;
    <label id="status"></label>
  </div>
</form>

1 个答案:

答案 0 :(得分:0)

编辑文本区域不会更改用于初始化文本区域的specialtyRequest对象。您需要调用JSON.parse将文本转换回对象。

var initialParams = [{
  "memberid": "etyhdjhsYYhajdsjdsQ==",
  "prescriptionInfo": [{
    "dispensingStoreDetails": {
      "dispensingStoreNumber": "1",
      "dispensingStoreNPINumber": "12302"
    }
  }]
}]

function init() {
  document.getElementById("spltyRxDetails").value = JSON.stringify(initialParams);
}
var specialtyRequest = {
  "lineOfBusiness": "SPECIALTY",
  "rxOrder": {
    "prescriptionDetails": []
  }
}
var requestParam = {
  orders: []
};
specialtyRequest.rxOrder.prescriptionDetails = initialParams;
requestParam.orders = specialtyRequest;
window.onload = init;

function execWrapperApi() {
  requestParam.orders = JSON.parse(document.getElementById("spltyRxDetails").value);
  console.log(requestParam.orders);
}
<form>
  <div class="form-group memberInfo">
    <label for="client_id">Client Id</label>
    <input type="text" class="form-control" id="client_id" placeholder="Enter a Client Id">
  </div>
  <div class="form-group">
    <div class="spltyParams" style="border:1px solid rgb(242, 255, 0); width:100px; length:100px">
      <label for="spltyRxDetails">validate Order - Request Parameters</label>

      <textarea type="text" class="form-control text-area-request" id="spltyRxDetails" placeholder="Enter Validate Order - Request Parameters in JSON"></textarea>
    </div>
    <button type="button" class="btn btn-primary" onclick="execWrapperApi()">Submit</button> &nbsp;
    <label id="status"></label>
  </div>
</form>