我接管了我公司网站的开发。当在card_filter div中更改选择框值时,将运行以下javascript。
$("#cards_filter select").change(function(e) {
var form = $(this);
var serdata = form.serialize();
var field_name = $(this).attr("name");
var field_value = $(this).val();
//Reset the selectors and keep the one that triggered filter
$('#cards_filter').find('select').val(function(index,value){
if($(this).attr("name") == field_name){
return field_value;
} else {
return "-1";
}
});
}
我想这样,当产品选择选项没有重置时,而收集,艺术家和类别选择框确实被重置。
<form id="cards_filter" method="post" action="" name="cards_filter">
<?php echo get_product_selector("products_filter"); ?>
<?php echo get_collection_selector("products_filter"); ?>
<?php echo get_artist_selector("products_filter"); ?>
<?php echo get_category_selector("products_filter"); ?>
</form>
以下函数在库中为每个选择器
function get_product_selector($form_name) {
if (isset($_SESSION['loggedin']['cur_product'])) {
$curr = $_SESSION['loggedin']['cur_product'];
} else {
$curr = -1;
}
$rtn = "";
$rtn .= '<select id="product" name="product">';
$rtn .= '<option value="-1"';
if ($curr == -1) {
$rtn .= ' selected';
}
$rtn .= '>Select a Product</option>';
$DB = new mysql_db();
$DB->sql_connect();
$q = "SELECT * FROM type order by type_title asc";
$DB->query($q);
if ($DB->get_num_rows()) {
$results = $DB->fetch_all_rows(MYSQL_ASSOC, NULL);
foreach ($results as $key => $row) {
$rtn .= '<option value="' . $row['Type_ID'] . '"';
if ($curr == $row['Type_ID']) {
$rtn .= ' selected';
}
$rtn .= '>' . trim($row['Type_Title']) . '</option>';
}
}
$rtn .= '</select>';
return $rtn;
}
答案 0 :(得分:2)
$("#cards_filter select").on('change', function() {
var serdata = $(this.form).serialize(),
field_name = this.name,
field_value = this.value;
$("#cards_filter select").not('#product').val(function(index, value) {
return this.name==field_name ? field_value : -1;
});
});
我不确定是否已获得,但更改事件位于选择框中,但您正在序列化this
,这不是表单而是选择?此外,它看起来“产品”选择具有ID #product
,所以只需从您更改值的选择器中排除它?