1

私は Javascript と JQuery を初めて使用することから始めます。私が達成したいのは、変数の値を画面に出力してからMySQLデータベースに追加するファイル内のdbQuery関数を呼び出すHTMLページに送信ボタンを配置することです。.js

selectedVisibleValue最初の関数で定義されているJavaScript 変数を使用する必要があるdbQuery理由は、4 つのドロップダウンがあり、そのうちの 3 つが非表示のドロップダウンであり、最初の非表示のドロップダウンに応じてのみ表示されるためです。非表示のドロップダウンが常に表示されます。

PHP ページでこれらの変数をformPage操作して、データベース機能を実行したいと考えています。私のコードは以下のとおりです。testing1関数を関数に追加したいですdbQuery

コピーして関数に貼り付けようとしましたが、dbQuery機能しません。selectedVisibleValue以下のコードでを操作しようとしているわけではありません。いくつかの偽の変数を使用してテストを行おうとしています。

    var dbQuery = function(){
    var description = document.getElementById("jobDescription").value;
    var selectedEquip = document.getElementById("equipmentList");
    var selectedEquip1 = selectedEquip.options[selectedEquip.selectedIndex].text;
    var selectedVisibleValue = $(".unitDropDowns select:visible").val();
    document.getElementById("descriptionSummary").innerHTML = "<h3>Description</h3>" + "<p>" + description + "</p>";
    document.getElementById("equipmentRan").innerHTML = "<h3>Equipment Ran </h3>" + "<p>" + selectedEquip1 + "</p>" + "<h3>Unit Number</h3>" + "<p>" + selectedVisibleValue + "</p>";
    document.getElementById("equipmentRan").style.display = "block";
    document.getElementById("descriptionSummary").style.display = "block";
}

var testing1 = function() {
    $.get(
        "formPage.php",
     {paramOne : 123, paramX : 'abc'},
     function(data) {
     document.getElementById("equipmentRan").innerHTML = ('page content: ' + data);
    }
);
}
4

3 に答える 3

4
//cache references to static elements
var jobDescription = $('#jobDescription')
  , selectedEquip = $('#equipmentList')
  , descriptionSummary = $('#descriptionSummary')
  , equipmentRan = $('#equipmentRan')
  ;    


function dbQuery(){

    //gather params    
    var params = {
        jobDescription : jobDescription.val(),
        selectedEquip1 : selectedEquip.val(),
        selectedVisibleValue = $(".unitDropDowns select:visible").val()
    }

    //show summary
    descriptionSummary.html('<h3>Description</h3><p>'+description+'</p></h3>').show();
    equipmentRan.html('<h3>Equipment Ran</h3><p>'+selectedEquip1+'</p><h3>Unit Number</h3><p>'+selectedVisibleValue+'</p>').show();

    //do a get
    $.get('formPage.php',params,function(data) {
        equipmentRan.html('page content: ' + data);
    }

}
于 2013-01-05T03:37:16.267 に答える
2

jsFiddle デモ

関数間で変数を渡すと、プロジェクトに役立つ場合があります。

HTML:

<div id="theBox"></div>
<button>Press Me</button>

JS

$(document).ready(function() {

    // This is some other Do More function, defined prior to the next variable function.
    // This is your .get() request.
    function doMore(target){
        // For the incomming targer, add a class style of a larger font.
        $(target).css('font-size', 30);
    }    


    // The main function.    
    var dbQuery = function() {
        // Show dynamic text on the HTML page.
        var extra = $('#theBox').html('Dynamic Text Results');
        // Run some other function, also... send the private variable in use.
        doMore(extra);        
    };


    // The submit button.
    $('button').on('click', function() {
        // Start the function.
        dbQuery();
    });

});
于 2013-01-05T03:56:18.560 に答える
0

作業コードは次のとおりです。

        function dbQuery() {
    window.description = document.getElementById("jobDescription").value;
    var selectedEquip = document.getElementById("equipmentList");
    window.selectedEquip1 = selectedEquip.options[selectedEquip.selectedIndex].text;
    window.selectedVisibleValue = $(".unitDropDowns select:visible").val();
    testing1();
}

function testing1() {
    $(document).ready(function() {
        $.get(
        "formPage.php",
     {paramOne : window.selectedVisibleValue, paramX : window.description, paramY : window.selectedEquip1},
     function(data) {
     document.getElementById("equipmentRan").innerHTML = (data);
    }
);
});
}
于 2013-01-05T04:33:38.143 に答える