这是一个下拉选择框,用户可以在其中选择页面,然后单击“提交”按钮转到该页面。 我正在使用Django 2.2,不知道如何使按钮转到所选链接
gradovi.html
{% block content %}
<div class="row">
<form>
<div class="input-field">
<select>
<option value="" disabled selected>Izaberite grad</option>
{% for grad in gradovi %}
<option value="{{grad.grad_slug}}">{{grad.grad_ime}}</option>
{% endfor %}
</select>
<label>Materialize Select</label>
</div>
<button class="btn waves-effect waves-light" type="submit" name="select">
<i class="material-icons right">send</i>
</button>
</form>
</div>
{% endblock %}
感谢@Diego Avila,这是最终代码
{% block content %}
<div class="row">
<div class="input-field">
<select id="select">
<option value="" disabled selected>Izaberite grad</option>
{% for grad in gradovi %}
<option value="{{grad.grad_slug}}">{{grad.grad_ime}}</option>
{% endfor %}
</select>
</div>
<button class="btn" onclick="redirectToMyPage();"><i class="material-icons">send</i></button>
</div>
<script>
function redirectToMyPage(){
location.href = document.getElementById('select').value;
}
</script>
{% endblock %}
答案 0 :(得分:1)
也许您可以尝试这样的事情:
1 .- (使用事件更改将redirec更改为网址):
<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option value="">Select...</option>
<option value="http://google.com">Google</option>
<option value="http://yahoo.com">Yahoo</option>
</select>
在此处检测到该选项,并获取用于重定向网址的值。
另一个带有按钮si的选项创建了一个自定义JavaScript函数来获取该值并重定向:
2 .- ,带有自定义功能JavaScript:
function redirectToMyPage(){
optionSelected = document.getElementById('mySelect').value;
//redirec to my url
window.location.replace(optionSelected);
}//end function redirectToMyPage
<select id="mySelect">
<option value="" disabled selected>Select...</option>
<option value="http://google.com">Google</option>
<option value="http://yahoo.com">Yahoo</option>
</select>
<button onclick="redirectToMyPage();">Go !</button>
在这种情况下,我得到选择的选项的值,然后重新制作URL,有关更多详细信息,请检查以下内容: Redirect to URL
祝你好运.. !!