我有三个下拉菜单:
<?php
if(isset($som)) {
=======first drop down=======
echo "<select id="."som-patch1"." class="."form-control".">";
echo "<option value="."select".">Please Select SOM patch...</option>";
for ($i=0; $i < count($som) ; $i++) {
echo '<option value="'.$som[$i]['som'].'">'.$som[$i]['som'].' </option>';
}
echo "</select>";
=======second drop down=======
echo "<select id="."som-patch1-month"." class="."form-control".">";
echo "<option value="."select".">Please Select Month ...</option>";
for ($i=0; $i < 12 ; $i++) {
$month = date('F', strtotime('-'.$i.' month', strtotime(date('Y-m-01'))));
echo '<option value="'.$month.'">'.$month.'</option>';
}
echo "</select>";
=======third drop down=======
echo "<select id="."som-patch1-year"." class="."form-control".">";
echo "<option value="."select".">Please Select Year ...</option>";
for ($i=0; $i < 12 ; $i++) {
$year = date('Y', strtotime('-'.$i.' year', strtotime(date('Y-m-01'))));
echo '<option value="'.$year.'">'.$year.'</option>';
}
echo "</select>";
}
?>
我可以成功地将选中的值发送到以下JavaScript function
:
$('[id^=som-patch1]').on('change', function() {
alert( this.value );
});
我的问题是:
我无法在变量中设置相关值,以便我可以调用add_som_donut_chart_org_data()
方法。此外,我需要确保在函数调用之前选择所有三个值。
我已经试过了:
$('[id^=som-patch1]').on('change', function() {
var som = null;
var month = null;
var year = null;
if (id=='som-patch1'){
som = this.value;
}
alert( som );
});
但是这不会设置som值。期望的结果应该是:
if (som !== null && month !== null && year !== null) {add_som_donut_chart_org_data(som,month,year)}
提前感谢。
UPDATE:以下代码解决了这个问题
$('[id^=som-patch1]').on('change', function() {
var id = $(this).attr("id");
if (id == 'som-patch1') {
som = this.value;
}
if (id == 'som-patch1-month') {
month = this.value;
}
if (id == 'som-patch1-year') {
year = this.value;
}
if (som !== null && month !== null && year !== null) {
add_som_donut_chart_org_data(som,month,year);
}
});
根据您当前的代码条件,if (id=='som-patch1')永远不会为真,因为id尚未定义。因此,它不会设置任何变量。原因很简单,您没有在代码中定义id变量。在您的条件之前,请使用以下代码:
var id = $(this).attr("id");