django 分类搜索(根据不同的单选框,改变form提交的地址)

时间:2024-12-21 23:33:38

前端html部分form

<form id="searchform" action="#" method="get"  class="navbar-form navbar-left">
<label class="radio-inline">
<input type="radio" name="searchtype" value="soft" checked>
<span style="color: white">软件</span>
</label>
<label class="radio-inline">
<input type="radio" name="searchtype" value="shop">
<span style="color: white">店铺</span>
</label>
<label class="radio-inline">
<input type="radio" name="searchtype" value="task">
<span style="color: white">任务</span>
</label>
<div class="form-group" style="margin-left: 5px">
<input type="text" name="keyword" class="form-control" placeholder="软件/店铺/任务">
</div>
<button type="button" id="searchbutton" class="btn btn-default">搜全站</button>
</form>

前端js部分

<script>
$(document).ready(function(){
$("#searchbutton").click(function(){
var a = $("input[name='searchtype']:checked").val();
if(a=="soft"){var newUrl = "{% url 'goods:softlist' %}";}
if(a=="shop"){var newUrl = "{% url 'goods:shoplist' %}";}
if(a=="task"){var newUrl = "{% url 'goods:tasklist' %}";}
//设置新提交地址
$("#searchform").attr('action',newUrl); //通过jquery为action属性赋值
$("#searchform").submit(); //提交ID为myform的表单
})
}); </script>