1

我已经从jQuery Querybuilder生成了 json 数据,我想用它来自动附加事件侦听器,但是在尝试了几个小时之后,我还没有找到办法。

这是 html 表单元素的 querybuilder json:

  let json = {
    "condition": "OR",
    "rules": [
      {
        "id": "poll_8002",
        "field": "Question 1",
        "type": "integer",
        "input": "radio",
        "operator": "equal",
        "value": 2
      },
      {
        "id": "poll_8002",
        "field": "Question 1",
        "type": "integer",
        "input": "radio",
        "operator": "equal",
        "value": 4
      }
    ],
    "valid": true,
    "then": [
      {
        "id": "field-poll_8002",
        "type": "single",
        "action": "hide"
      }
    ]
  }

这是我尝试使用 json 生成的代码:

  $('input[name="poll_8002"]').on("click change", function () {
    var val = $(this).val()
    if (val === '2' || val === '4') {
      $('.field-poll_8002').hide()
    } else {
      $('.field-poll_8002').show()
    }
  })

是否有任何包/脚本可以帮助自动执行此操作,而不是使用 if/else 语句进行编码?我怎样才能做到这一点?

4

0 回答 0