[英]Reset select box value
I'm trying to make a reset button with jquery. 我正在尝试使用jquery进行重置按钮。 I'm calling a function named cleanFilters when the button is clicked.
单击按钮时,我正在调用一个名为cleanFilters的函数。 But the console tells me cleanFilters is not defined.
但是控制台告诉我cleanFilters没有定义。 I'm very new in Javascript, so I guess I'm missing something stupid.
我是Java语言的新手,所以我想我缺少了一些愚蠢的东西。 I don't know if it matters but I have to say that the select I'm using is from Foundation 5.
我不知道这是否重要,但是我不得不说我正在使用的选择来自Foundation 5。
This is my code 这是我的代码
<script>
$(function cleanFilters() {
$('#select-filter-2').val('cualquier_provincia');
$('#select-filter-2').trigger('change', true);
});
</script>
<select name="" id="select-filter-2">
<option value="cualquier_provincia">Cualquier provincia</option>
<option value="A Coruña">A Coruña</option>
</select>
<button id="reset" onclick="cleanFilters()">Limpiar filtros de búsqueda</button>
You shoud wrap the function definition inside an anonymouns one, not directly between the parenthesis: $(function(){/* here goes the code included function definitions * /}) 您应该将函数定义包装在一个圆括号中,而不是直接放在括号之间: $(function(){/ * 这里是包含函数定义* /} 的代码 )
Moreover when you define the cleanFilters function, it gets tied to the $(function(){}) scope where it is defined, that's why it is not accesible from onclick. 此外,当您定义cleanFilters函数时,它会与定义它的$(function(){})范围相关联,这就是为什么onclick无法使用它的原因。
From my point of view you have two options: 从我的角度来看,您有两种选择:
add this after function definition and remove the onclick 在函数定义之后添加此代码并删除onclick
$('#reset').click(function(){ cleanFilters(); }) $('#reset')。click(function(){cleanFilters();})
or 要么
Here is a working version http://jsbin.com/habiyubamo/edit 这是一个工作版本http://jsbin.com/habiyubamo/edit
Found syntax errors, fixed and shown below 已发现语法错误,已修复,如下所示
<script src="https://code.jquery.com/jquery-2.1.4.js"></script> <select name="" id="select-filter-2"> <option value="cualquier_provincia">Cualquier provincia</option> <option value="A Coruña">A Coruña</option> </select> <button id="reset" onclick="cleanFilters()">Limpiar filtros de búsqueda</button> <script> function cleanFilters() { $('#select-filter-2').val('cualquier_provincia'); $('#select-filter-2').trigger('change', true); } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.