0

我对 JavaScript 很陌生,对 JavaScript 中的数组有疑问。直截了当:我有 HTML 代码:

<input type="text" name="conPrice" id="conPrice" size="16" maxlength="128"/>
<input type="text" name="OverP" id="OverP" size="16" maxlength="128"  />
<div id="slider"></div> //Slider

数量的第一个字段,从滑块中拉出。SQL 表中价格的第二个字段

SQL基于有四个字段的条件:

| ID | MIN | MAX | PRICE |

我试图根据我的滑块值在字段OverP中获取价格列价格。因此,例如,如果我在滑块上有数字 6,并且它在 1-7 之间作为 MIN 和 MAX,那么价格将为 10。

我有这个 SQL:

<?php
$x = mysql_query("SELECT p_max AS max FROM dsd_price");
$y = mysql_query("SELECT p_min AS min FROM dsd_price");
$z = mysql_query("SELECT p_price AS price FROM dsd_price");
while ($x_array = mysql_fetch_array($x)){$max[] = $x_array;}
while ($y_array = mysql_fetch_array($y)){$min[] = $y_array;}
while ($z_array = mysql_fetch_array($z)){$price[] = $z_array;}
?>

最后是我的 JS:

<script>
$(document).ready(function slider() {
$( "#slider" ).slider({
    value:1,
    min: 0,
    max: 201,
    step: 1,
    slide: function( event, ui ) {
//Its setting the slider value to the element with id "conPrice" 
$("#conPrice" ).val(ui.value);
    }
});
});

$(document).ready(function calcul() {
var frm = document.dsd_form;
var quant = frm.conPrice;
var overp = ['<?php echo implode("','", $price); ?>'];
var mini = ['<?php echo implode("','", $min); ?>'];
var maxi = ['<?php echo implode("','", $max); ?>'];
for(index = 0; quant > mini[index], quant < maxi[index]; index++){
    $( "#OverP" ).val( overp[index] );
})
}   
</script>

我没有得到我希望的结果,我知道问题出在我的 JS 上。希望尽快对我的话题提出一些建议。

更新:到目前为止放入测试页面的整个代码。滑块确实滑动,滑块值正在处理为 conPrice,但计算不起作用...

<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<!-- For slider -->
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>

<!-- Slider JS -->
<?php
$x = mysql_query("SELECT p_max AS max FROM dsd_price");
$y = mysql_query("SELECT p_min AS min FROM dsd_price");
$z = mysql_query("SELECT p_price AS price FROM dsd_price");
while ($x_array = mysql_fetch_array($x)){$max[] = $x_array['max'];}
while ($y_array = mysql_fetch_array($y)){$min[] = $y_array['min'];}
while ($z_array = mysql_fetch_array($z)){$price[] = $z_array['price'];}
?>
<script>
$(document).ready(function slider() {
$( "#slider" ).slider({
    value:1,
    min: 0,
    max: 201,
    step: 1,
    slide: function( event, ui ) {
  //Its setting the slider value to the element with id "conPrice" 
  $("#conPrice" ).val(ui.value);
    }
 });
    function price(){
    var frm = document.dsd_form;
    var quant = frm.conPrice;
    var overp = ['<?php echo implode("','", $price); ?>'];
    var mini = ['<?php echo implode("','", $min); ?>'];
    var maxi = ['<?php echo implode("','", $max); ?>'];
    for(var index = 0; quant > mini[index], quant < maxi[index]; index++){
        $( "#OverP" ).val( overp[index] );
    }
}
});
</script>
</head>
<body>
<form id="dsd_form" name="dsd_form" action="db_create_sql.php" method="post" class="niceform">
<fieldset>
       <dl>
            <dt><label for="conPrice">Price:</label></dt>
            <dd><input type="text" name="conPrice" id="conPrice" size="16" maxlength="128" />
            <input type="text" name="OverP" id="OverP" size="16" maxlength="128" />
            <div id="slider" ></div>
            </dd>
        </dl>
</fieldset>
</form>
</body>
</html>

拥有 SQL 数据库:

p_id | p_min | p_max | p_price
  1        1      1       11
  2        2      5       13
4

1 回答 1

0

看来您的括号与 calcul() 函数不匹配。尝试这个:

$(document).ready(function calcul() {
var frm = document.dsd_form;
var quant = frm.conPrice;
var overp = ['<?php echo implode("','", $price); ?>'];
var mini = ['<?php echo implode("','", $min); ?>'];
var maxi = ['<?php echo implode("','", $max); ?>'];
for(index = 0; quant > mini[index], quant < maxi[index]; index++)
{
    $( "#OverP" ).val( overp[index] );
}});
于 2012-10-02T18:13:27.140 回答