0

3 つのフィールドでクエリを実行しようとしています。3 つのコンボボックスがあり、それぞれにフィールド値が入力されています。各フィールドのコンボボックスから選択した値を使用してクエリを実行したいです。3 つのフィールドを収集する方法を知りたいです。どこの条項?アイデアはありますか?

4

2 に答える 2

0
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>

var si = document.getElementById("mySelect").selectedIndex;
var arrayOfOptionsy=document.getElementById("mySelect").options;
//alert("Index: " + arrayOfOptionsy[si].index + " is " + arrayOfOptionsy[si].text);

query.where = "fruit = '" + arrayOfOptionsy[si].text + "' AND not_gis = '"...
于 2013-05-13T23:24:31.923 に答える
0

上記の答えは素晴らしいです。この種のことを処理するためのいくつかのツールを強調したいと思います。

jQuery

jQuery インターフェースにより、JavaScript での DOM の操作がはるかに簡単になります。たとえば、次のフォームがあるとします。

<label>Latitude<input id="latitude" type="number" step=".01" min="-90" max="90 /></label>
<label>Longitutde<input id="longitutde" type="number" step=".01" min="-180" max="180" /></label>
<select id="radius">
    <option>1 km</option>
    <option>5 km</option>
    <option>20 km</option>
</select>

次に、次のように where 句を作成する際に使用するフォーム内の現在の情報を取得できます。

var lat = $('#latitude').val();  // $('#latitude') is the element with id 'latitude'
var lon = $('#longitude').val(); // and .val() grabs the current value of that input
var rad = $('#radius').val();    // and this syntax works with various input elements

ノックアウト

Knockout フレームワークを使用すると、DOM 要素を JavaScript 変数に宣言的にリンクできるため、DOM でそれらを変更すると、JavaScript ですぐに変更されます。また、必要に応じてデータを再処理できる変更イベントを取得できます。この例では、フィールドが変更されるたびにデータベースにクエリを実行することができます。方法は次のとおりです。

<label>Latitude<input data-bind="value: lat" type="number" step=".01" min="-90" max="90 /></label>
<label>Longitutde<input data-bind="value: lon" type="number" step=".01" min="-180" max="180" /></label>
<select data-bind="options: ['1 km', '5 km', '20 km'], value: rad"></select>

Knockout では、'data-bind="value: var"' を使用して、DOM 要素にバインドする変数を選択します。JavaScript には、次のものがあります。

var queryModel = {          // we create the "data model" to be used in the DOM
    lat: ko.observable(0),  // and wrap each value in a ko.observable(); we can 
    lon: ko.observable(),   // assign default values or leave them empty
    rad: ko.observable(),
    query: ko.computed(function() {
        // query processing here; to access the variable values use this.lat(),
        // this.lon(), this.rad(); you can then access this variable in JavaScript
        // using queryModel.query()
    }, this)
};

queryModel.query.subscribe(function(query) {
    // this function will be called every time the query variable is recomputed; you
    // may add code here that would run this query every time the query updates
});

ko.bind('queryModel'); // and finally, we "bind" the data model to the DOM

これは明らかに jQuery モデルよりも少し複雑ですが、DOM で更新されたデータを反応的に処理できるため、強力なツールです。

于 2013-05-14T14:43:47.723 に答える