我需要一些帮助,我想知道如何将值从PHP变量传递给jquery脚本? 我正在做的是从一个mysql浏览创建的元素列表中打开一个模态窗口,所以我需要传递一个变量值。 这是我的代码:
<?php
$query_tours = "Select * from tours where feautured = 'Y' " ;
$result_tours = mysql_query($query_tours);
while($row=mysql_fetch_array($result_tours )) {
$tourID = $row['recid'];
$tit_esp = $row['tit_esp'];
$adrate = $row['ad_rate'];
$chrate = $row['ch_rate'];
echo '<div style="border:1px solid #EEE;font-size:12px">';
print "$tit_esp <br>";
print "Adultos : $adrate | Niños : $chrate ";
print "<a href='#?tourid=$tourID' class='myTour_$tourID' >ver detalles</a>";
print "</div>";
}
&GT;
这里是jquery脚本:
<script>
$(function() {
$( "#dialog:ui-dialog" ).dialog( "destroy" );
var adult = $( "#adult" ),
child = $( "#chl" ),
fecha = $("#datepicker"),
allFields = $( [] ).add( adult ).add( child ).add( fecha ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
function checkLength( o, n, min, max ) {
if ( o.val()> max || o.val()< min ) {
o.addClass( "ui-state-error" );
updateTips( "El numero de " + n + " debes ser entre " +
min + " y " + max + "." );
return false;
} else {
return true;
}
}
function checkFecha() {
var dfecha = document.getElementById('datepicker').value;
if(dfecha == ""){
//dfecha.addClass( "ui-state-error" );
updateTips( "Fecha Incorrecta");
return false;
}else{
return true;
}
}
function checkRegexp( o, regexp, n ) {
if ( !( regexp.test( o.val() ) ) ) {
o.addClass( "ui-state-error" );
updateTips( n );
return false;
} else {
return true;
}
}
$( "#dialog-form" ).dialog({
position: 'center',
autoOpen: false,
draggable: true,
height: 300,
width: 410,
modal: true,
show : {
transitionIn: 'blind',
transitionOut: 'explode',
},
open: function(event, ui) {
$( "#datepicker" ).datepicker('enable');
alert(regis)
$.ajax({
url: 'tour_cont.php',
data: "regis=" + regis + "",
dataType: 'json',
success: function(data){
var titulo = data[1];
var user = data[3];
var comenta = data[4];
$( "#dialog-form" ).append( "<tr>" +
"<td width='21%'>" + titulo + "</td>" + "</tr>" );
}
});
},
buttons: {
"Agregar al Carrito": function() {
var bValid = true;
bValid = bValid && checkFecha( datepicker );
bValid = bValid && checkLength( adult, "adultos", 1, 10 );
bValid = bValid && checkLength( child, "niños", 0, 10 );
if ( bValid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + adult.val() + "</td>" +
"<td>" + child.val() + "</td>" +
"<td>" + fecha.val() + "</td>" +
"</tr>" );
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
$('#datepicker').datepicker('disable');
}
});
$('a[class^=myTour]')
.button()
.click(function() {
$( "#dialog-form" ).dialog( "open" );
});
$( "#create-chichen" )
.button()
.click(function() {
$( "#dialog-form" ).dialog( "open" );
});
});
</script>
希望有人能够理解我,先谢谢你们!
答案 0 :(得分:3)
你可以将php值传递给像这样的javascript
<script type="text/javascript">
var myJSval = <?php echo $phpVal; ?>;
</script>