简体   繁体   English

使用jQuery获取输入,然后搜索数组,然后如果我的值在数组中,则输出所需的答案

[英]Using jQuery to get an input then searching through an array, then if I value is in array, output the desired answers

SO, I'm relatively new to arrays. 所以,我对数组还比较陌生。 I've tried to solve this with a few variations. 我尝试通过一些变体来解决这个问题。

The basis of what I'm trying to do is in the title, but I'll try and explain in more detail. 我想要做的事情的基础是标题,但我将尝试更详细地解释。

I have a selection of PDFs, each PDF will be output for selection. 我有一些PDF,每个PDF将输出以供选择。 Each PDF has an array to represent it, the array contains answers from questions that the user can select an answer for to specify which PDF suits their needs. 每个PDF都有一个表示它的数组,该数组包含问题的答案,用户可以选择答案以指定哪个PDF满足他们的需求。

I have built a form -- 我已经建立了一个表格-

<form class="form form-vertical specifier" method="post">

    <div class="control-group">
        <div class="input-append">
            <span class="add-on">Q1</span>
        </div>
        <label class="control-label">What is the Sub-Floor?</label>
        <select name="subFloor">
            <option value="">Please Choose</option>
            <option value="1a">Cementious Screed</option>
            <option value="1b">Anhydrite/Calcium Sulphate Screeds</option>
            <option value="1c">Timber/Chipboard</option>
            <option value="1d">Steel</option>
            <option value="1f">Selecta Screenbooard</option>
            <option value="1e">Any Other?</option>
        </select>
    </div>
    <div class="control-group">
        <div class="input-append">
            <span class="add-on">Q2</span>
        </div>
        <label class="control-label">Is there a weight issue?</label>
        <select name="weight">
            <option value="">Please Choose</option>
            <option value="2a">Yes</option>
            <option value="2b">No</option>
        </select>
    </div>

    <div class="control-group">
        <div class="input-append">
            <span class="add-on">Q3</span>
        </div>
        <label class="control-label">Is tehre a height issue?</label>
        <select name="height">
            <option value="">Please Choose</option>
            <option value="3a">Yes</option>
            <option value="3b">No</option>
        </select>
    </div>

    <div class="control-group">
        <div class="input-append">
            <span class="add-on">Q4</span>
        </div>
        <label class="control-label">Is there under-floor heating?</label>
        <select name="heating">
            <option value="">Please Choose</option>
            <option value="4a">Yes</option>
            <option value="4b">No</option>
        </select>
    </div>

    <div class="control-group">
        <div class="input-append">
            <span class="add-on">Q5</span>
        </div>
        <label class="control-label">What is the Sub-Floor</label>
        <select name="damp">
            <option value="">Please Choose</option>
            <option value="5a">Yes</option>
            <option value="5b">No</option>
        </select>
    </div>

    <div class="control-group">
        <div class="input-append">
            <span class="add-on">Q6</span>
        </div>

        <select name="acoustic">
            <option value="">Please Choose</option>
            <option value="6a">Yes</option>
            <option value="6b">No</option>
        </select>
    </div>

    <input type="submit" name="submit" >

</form>

The form values represent the question number and then a unique letter that can be associated in an array. 表单值表示问题编号,然后表示可以在数组中关联的唯一字母。

The arrays look like this -- 数组看起来像这样-

$pdfs = array( 
    array('pdf01', 'a', 'a', 'a', 'b', 'a', 'a'),
    array('pdf02', 'a', 'a', 'a', 'b', 'a', 'a'),
    array('pdf03', 'a', 'a', 'a', 'a', 'b', 'a'),
    array('pdf04', 'a', 'a', 'a', 'a', 'a', 'a'),
    array('pdf05', 'a', 'b', 'b', 'a', 'b', 'b'),
    array('pdf06', 'c', 'a', 'a', 'b', 'b', 'a'),
    array('pdf07', 'a', 'a', 'a', 'b', 'b', 'a'),
    array('pdf08', 'f', 'b', 'b', 'b', 'b', 'b'),
    array('pdf09', 'f', 'b', 'b', 'b', 'b', 'b'),
    array('pdf10', 'a', 'b', 'b', 'b', 'a', 'b'),
    array('pdf11', 'a', 'b', 'a', 'b', 'a', 'b'),
    array('pdf12', 'b', 'b', 'b', 'a', 'b', 'b'),
    array('pdf13', 'g', 'b', 'b', 'a', 'a', 'b'),
    array('pdf14', 'a', 'b', 'b', 'a', 'a', 'b'),
    array('pdf15', 'd', 'a', 'a', 'b', 'b', 'a')
);`

From this I need to be able to output the PDF name if the input value matches what's in the array. 由此,如果输入值与数组中的值匹配,我需要能够输出PDF名称。

Any help or pushes in the right direction would be extremely appreciated. 朝正确方向的任何帮助或推动将不胜感激。

fyi, I'm trying to keep this dynamic so, 菲,我正在努力保持这种动态,

$('selector').change(function() {
}) 

is, I'm presuming, the approach I need. 我想是我需要的方法。

I'd create a json instead of an array. 我会创建一个json而不是一个数组。 Something along the line of 沿线的东西

jsonObj = { aaabaa: 'pdf01', aaabab: 'pdf02', .... }

Then you can just put your answers together as a string and look it up in the json object: 然后,您可以将答案作为一个字符串放在一起,然后在json对象中查找它:

var pdf = jsonObj[answerString]

For finding all PDFs matching a non complete string it gets a little complicated: 要查找所有与不完整字符串匹配的PDF,会有些复杂:

  1. Build a regular expression. 建立一个正则表达式。 For each not given answer, insert \\w. 对于每个未给出的答案,请插入\\ w。 So that it looks like this: var regEx = /aa\\wb\\w/ (read as a, a, not set, b, not set). 这样看起来像这样: var regEx = /aa\\wb\\w/ (读为a,a,未设置,b,未设置)。 To dynamically construct a regular expression you first create it as a string with escaped backslashes: var regExString = "aa\\\\wb\\\\w" in this case. 要动态构造正则表达式,您首先将其创建为带有转义反斜杠的字符串: var regExString = "aa\\\\wb\\\\w"在这种情况下。 Then give it to a new RegExp object: var regEx = new RegExp(regExString) 然后将其提供给新的RegExp对象: var regEx = new RegExp(regExString)
  2. Iterate through your object and check the keys: 遍历对象并检查键:

    var result = []; for (key in jsonObj) { if (regEx.test(key)) { result.push(jsonObj[key]); } }

And then you finally have an array ;) But only with the valid PDFs. 然后,您终于有了一个数组;)但仅包含有效的PDF。

But with this approach you lose the advantage of a fast and efficient solution. 但是使用这种方法,您将失去快速有效的解决方案的优势。 You could also iterate through your array then and pick only the valid elements. 您还可以遍历数组,然后仅选择有效元素。 It is just a little less code with this solution. 此解决方案的代码少了一点。

JSFiddle for a working demo. JSFiddle用于工作演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM