【问题标题】:Adding up to text javascript添加到文本javascript
【发布时间】:2021-07-06 16:58:41
【问题描述】:

我想根据用户提供的输入添加到现有文本中,

下面是我想要达到的目标

<html>
<p>Enter the IDs of devices seperated by comma</p>
<input id="devicesIDs"></input>
<script> 
            function ButtonClicked(devicesIDs): 
            var IDs = document.getElementById("devicesIDs").innerText;
            var inputArray = IDs.split(',');

            let obj = {
        "c8y_isDynamicGroup": {
        "invisible": true
    },
    "type": "c8y_DynamicGroup",
    "name": " SmartGroupSendMessageXXX",
    "hajjMessage": "messages typed in the widget",
    "c8y_DeviceQueryString": "$filter=((id eq '**{{entered ID1}}**') or (id eq '**entered ID2**')) or (id eq '**entered ID3**'))"
}
console.log(obj)
postData('......url....', obj)
    .then(data => {
        console.log(postData)
        console.log(data);
    }); 
            </script>

我需要在原文中添加“or (id eq 'entered ID3'))” $filter=((id eq '{{entered ID1}}') 如果输入了多个 ID,根据 ID 的数量,并在其中包含每个 ID

【问题讨论】:

    标签: javascript arrays input


    【解决方案1】:

    您已经在数组中获得了 ID,我们可以将它们格式化,然后将它们加入到过滤器查询中

      function ButtonClicked(devicesIDs):
        var IDs = document.getElementById("devicesIDs").innerText;
        var inputArray = IDs.split(',');
        let filterGroup = inputArray.map(id => `(id eq '${id.trim()}')`)
        //  to choose one at random
        filterGroup = `$filter=(${filterGroup[Math.floor(Math.random()*filterGroup.length)]})`;
        // to use all with OR
        // filterGroup = `$filter=(${filterGroup.join(" or ")})`;
        
        let obj = {
          "c8y_isDynamicGroup": {
            "invisible": true
          },
          "type": "c8y_DynamicGroup",
          "name": " SmartGroupSendMessageXXX",
          "hajjMessage": "messages typed in the widget",
          "c8y_DeviceQueryString": filterGroup
      }
    

    【讨论】:

    • 非常感谢您的成功!还有一种方法可以只选择一个随机ID吗?我想为(仅选择一个输入的列表)添加一个复选框,以便在选中此复选框时,它仅随机选择一个 id 而不是所有列表
    • 我添加了一种随机过滤器。如果此答案有助于解决您的问题,请将其标记为解决方案,谢谢!
    【解决方案2】:

    document.querySelector("button").onclick=ev=>{
      const IDs = document.getElementById("devicesIDs").value.split(','),
            obj = {
      "c8y_isDynamicGroup": {"invisible": true},
      "type": "c8y_DynamicGroup",
      "name": " SmartGroupSendMessageXXX",
      "hajjMessage": "messages typed in the widget",
      "c8y_DeviceQueryString": "$filter=("+IDs.map(id=>"(id eq '"+id.trim()+"')").join(" or ")+")"
      };
     fetch('https://jsonplaceholder.typicode.com/users/',  // placeholder for testing ...
       {method: 'POST',body: JSON.stringify(obj),
        headers: {'Content-type': 'application/json; charset=UTF-8'}})
     .then(r=>r.json()).then(data=>console.log(data))
    }
    <p>Enter the IDs of devices seperated by comma</p>
    <input id="devicesIDs" value="3,4,5">
    <button>go</button>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2014-08-02
      • 2015-02-02
      相关资源
      最近更新 更多