帮助链接选择和现有表单值,例如:添加

发布于 2024-10-31 06:21:30 字数 2725 浏览 0 评论 0原文

我有一个 3 步链式选择序列,game ->;战斗->获胜方 ,从 MySQL 数据库中提取所有数据。

在互联网上闲逛之后,我发现了一个紧凑的 jQuery 脚本,它的性能非常好。但是,我不知道如何使用此脚本允许现有数据:

chained select javascript:

<script>
var ajax = new Array();

function getScenNumList(sel)
{
    var game = sel.options[sel.selectedIndex].value;
    document.getElementById('scenarioNumber').options.length = 0;   // Empty scenario number select box
    if(game.length>0){
        var index = ajax.length;
        ajax[index] = new sack();

        ajax[index].requestFile = 'js/getPlayData.php?gameName='+game;  // Specifying which file to get
        ajax[index].onCompletion = function(){ createScenarioNumbers(index) };  // Specify function that will be executed after file has been found
        ajax[index].runAJAX();      // Execute AJAX function
    }
}

function createScenarioNumbers(index)
{
    var obj = document.getElementById('scenarioNumber');
    eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}

function getNations(sel)
{
    var scenNum = sel.options[sel.selectedIndex].value;
    document.getElementById('victor').options.length = 0;   // Empty nation select box
    if(scenNum.length>0){
        var index = ajax.length;
        ajax[index] = new sack();

        ajax[index].requestFile = 'js/getPlayData.php?scenID='+scenNum; // Specifying which file to get
        ajax[index].onCompletion = function(){ createNations(index) };  // Specify function that will be executed after file has been found
        ajax[index].runAJAX();      // Execute AJAX function
    }
}

function createNations(index)
{
    var obj = document.getElementById('victor');
    eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}

</script>

摘自 PHP 数据库检索脚本 (getPlayData.php):

$gameName = mysql_real_escape_string($_GET['gameName']);

$q = "SELECT a, b, c FROM table WHERE game='$gameName' ORDER BY num ASC";

$r = mysql_query($q);

echo "obj.options[obj.options.length] = new Option('#','');\n";

while ($row = mysql_fetch_row($r)) {

    $string = mysql_real_escape_string(($row[0].' - '.$row[1])); // needed so quotes ' " don't break the javascript

    echo "obj.options[obj.options.length] = new Option('$string','$row[2]');\n";

}

回显 obj.options 是此脚本使用的常用方法。这对我来说似乎很难看,但我不知道任何 JavaScript,所以我不想摆弄它。

HTML 非常简单,只是一个包含一些空

我的问题是这样的:对于新记录来说一切都很好,但我不知道如何改变它以支持将每个选择中的一个选项标记为选定的,假设我有该数据手(例如:用户正在编辑现有记录)?

非常感谢!

I have a 3 step chained-select sequence, game -> battle -> winning side , which pulls all data from a MySQL database.

After some wandering on the internet, I found a compact jQuery script that performs wonderfully. However, I am at a loss as to how to allow for existing data: <option selected="selected"></option> using this script.

chained select javascript:

<script>
var ajax = new Array();

function getScenNumList(sel)
{
    var game = sel.options[sel.selectedIndex].value;
    document.getElementById('scenarioNumber').options.length = 0;   // Empty scenario number select box
    if(game.length>0){
        var index = ajax.length;
        ajax[index] = new sack();

        ajax[index].requestFile = 'js/getPlayData.php?gameName='+game;  // Specifying which file to get
        ajax[index].onCompletion = function(){ createScenarioNumbers(index) };  // Specify function that will be executed after file has been found
        ajax[index].runAJAX();      // Execute AJAX function
    }
}

function createScenarioNumbers(index)
{
    var obj = document.getElementById('scenarioNumber');
    eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}

function getNations(sel)
{
    var scenNum = sel.options[sel.selectedIndex].value;
    document.getElementById('victor').options.length = 0;   // Empty nation select box
    if(scenNum.length>0){
        var index = ajax.length;
        ajax[index] = new sack();

        ajax[index].requestFile = 'js/getPlayData.php?scenID='+scenNum; // Specifying which file to get
        ajax[index].onCompletion = function(){ createNations(index) };  // Specify function that will be executed after file has been found
        ajax[index].runAJAX();      // Execute AJAX function
    }
}

function createNations(index)
{
    var obj = document.getElementById('victor');
    eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}

</script>

excerpt from the PHP database retrieval script (getPlayData.php):

$gameName = mysql_real_escape_string($_GET['gameName']);

$q = "SELECT a, b, c FROM table WHERE game='$gameName' ORDER BY num ASC";

$r = mysql_query($q);

echo "obj.options[obj.options.length] = new Option('#','');\n";

while ($row = mysql_fetch_row($r)) {

    $string = mysql_real_escape_string(($row[0].' - '.$row[1])); // needed so quotes ' " don't break the javascript

    echo "obj.options[obj.options.length] = new Option('$string','$row[2]');\n";

}

echoing the obj.options is the stock method this script was using. It seems ugly to me, but I don't know any javascript so I didn't want to fiddle with it.

The HTML is simple enough, just a table with a few empty <select> objects with IDs matching those in the javascript and onchange="getXXX(this)" calls.

My question is this: Everything works great for new records, but I'm at a loss as to how I can alter this to support marking one option from each select as selected, assuming I have that data in hand (ex: a user is editing an existing record) ?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

噩梦成真你也成魔 2024-11-07 06:21:30

您可以参阅本教程,了解如何创建默认选择的选项。 http://www.javascriptkit.com/javatutors/selectcontent.shtml 中的一个参数选项构造函数指示是否选择该选项。

在 PHP 文件中,您将进行如下编辑:

$gameName = mysql_real_escape_string($_GET['gameName']);

$q = "SELECT a, b, c FROM table WHERE game='$gameName' ORDER BY num ASC";

$r = mysql_query($q);

echo "obj.options[obj.options.length] = new Option('#','');\n";

while ($row = mysql_fetch_row($r)) {

    $string = mysql_real_escape_string(($row[0].' - '.$row[1])); // needed so quotes ' " don't break the javascript
    if ($string ......)
    echo "obj.options[obj.options.length] = new Option('$string','$row[2]', false, true);\n";
    else
     ......

}

You can see this tutorial for creating an option that is selected as default. http://www.javascriptkit.com/javatutors/selectcontent.shtml One parameter in the option constructor dictate that whether the option is selected or not.

In the PHP file you will edit as follow:

$gameName = mysql_real_escape_string($_GET['gameName']);

$q = "SELECT a, b, c FROM table WHERE game='$gameName' ORDER BY num ASC";

$r = mysql_query($q);

echo "obj.options[obj.options.length] = new Option('#','');\n";

while ($row = mysql_fetch_row($r)) {

    $string = mysql_real_escape_string(($row[0].' - '.$row[1])); // needed so quotes ' " don't break the javascript
    if ($string ......)
    echo "obj.options[obj.options.length] = new Option('$string','$row[2]', false, true);\n";
    else
     ......

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文