```javascript// 搜索跳转页面
function newSearch() {
// 设置搜索类型为索引
document.getElementById("flag").value = "index";
// 输入框获取查询关键字
var q = document.getElementById("q");
if (q.value === "") {
alert("请输入查询关键字!");
q.focus();
return false;
} else if (q.value === "商品名称、作者、出版社、ISBN") {
alert("请输入查询关键字!");
q.focus();
return false;
}
// 判断查询是书店去 or 拍卖区
var areaTit = $(".area_tit a.now").html();
if (areaTit === "拍卖区") {
document.getElementById("searchType").value = "pm";
} else if (areaTit === "书店区") {
document.getElementById("searchType").value = "shop";
}
// 判断查询状态
var selType = $($.sel).text();
}
```
```javascriptif (selType === "在售") {
document.getElementById("sale").value = "0";
} else if (selType === "已售") {
document.getElementById("sale").value = "1";
} else if (selType === "全部") {
document.getElementById("sale").value = "2";
} else if (selType === "在拍") {
document.getElementById("sale").value = "2";
} else if (selType === "已结束") {
document.getElementById("sale").value = "3";
}
var flag = document.getElementById("searchType").value;
var url = '';
if (flag === 'shop') {
var qVal = document.getElementById("q").value;
var saleVal = document.getElementById("sale").value;
qVal = KFZ.util.charToUnicode(qVal);
qVal = qVal.toLowerCase();
}
```
```javascript// 重构后的代码:
url = "http://search.kongfz.com/";
if (flag === 'z') {
url += 'product/';
url += 'z';
url += qVal;
url += 'y';
url += saleVal + '/';
window.open(url);
return false;
} else if (flag === 'pm') {
var selType = document.getElementById('sale').value;
if (selType === '3') {
// 重构前的代码
// url = "http://search.kongfz.com/auction.jsp?query=" + val + "&searchProperty=history&content=" + val + "&act=search";
var action = "http://search.kongfz.com/auction.jsp";
var query = encodeURI(document.getElementById('q').value);
// 重构后的代码
// url = action + '?&searchType=' + flag + '&query=' + query + '&searchProperty=history&act=search';
}
}
```
请将以下代码重构为一个函数,并保持段落结构:
```javascript
function search(query) {
qVal = KFZ.util.charToUnicode(query);
qVal = qVal.toLowerCase();
url = "http://search.kongfz.com/pm/z" + qVal + "/";
window.open(url);
return false;
}
function searchSale() {
var qVal = document.getElementById("q").value;
var saleVal = document.getElementById("sale").value;
url = "http://www.kongfz.cn/search_result/?sc=" + qVal;
window.open(url);
return false;
}
```
以下是重构后的代码:
```javascript
function setSelType() {
// 添加默认搜索类型
var selType = $('.sel').text();
if (selType === '' || selType.length === 0) {
$('.sel').text('在售');
}
// 去掉í
if ($('#q').val() === 'í') {
$('#q').addClass('default').val('商品名称、作者、出版社、ISBN');
}
}
setTimeout(setSelType, 200);
$(document).ready(function() {
$('input[type="text"]#q').keyup(function(e) {
if (e.keyCode === 13) {
// newSearch();
}
});
});
var searchView = new KFZ.views.SearchView();
// 联想
new KFZ.ui.AssInput({
input: '#q',
box: '.assBox',
getItems: function(val, setItems) {
if (val.trim() === '' || val === searchView.defaultText) return;
var url = 'http://search.kongfz.com/sug/suggest_server.jsp';
}
});
```
重构后的内容如下:
```javascript
data = {
"query": val
};
$.ajax({
url: url,
data: data,
type: "GET",
cache: false,
dataType: "script",
success: function() {
try {
// items = new Function("return " + items)();
items = sugWords;
// 将联想选项数据填充到选项盒子里
setItems(val, items);
} catch (e) {}
}
});
callback: function() {
$("#btn_search").click();
}
```