javascript依赖下拉与doctrine表单元素

时间:2016-11-22 17:10:35

标签: javascript doctrine-orm zend-framework2

我试图使用javascript jquery做一个下拉列表。 使用zend framework2和doctrine,我将html元素呈现如下:

<div>           
Country<select name="country"><option value="">--select Country-</option>
<option value="1" class="country" id="country">Greater Accra</option>
<option value="2" class="country" id="country">Ashanti</option>
<option value="3" class="country" id="country">Central</option>
<option value="4" class="country" id="country">Eastern</option></select>           
</div>

<div>       
States<select name="states"><option value="">--select States--</option>
<option value="1" class="states" id="states" data-id="1">Roman      Ridge</option>
<option value="2" class="states" id="states" data-id="2">Kumasi</option>
<option value="3" class="states" id="states" data-id="3">Cape Coast</option>
<option value="4" class="states" id="states" data-id="4">Koforidua</option>
<option value="5" class="states" id="states" data-id="1">Accra</option>
<option value="6" class="states" id="states" data-id="3">Moree</option>
<option value="7" class="states" id="states" data-id="1">Chantan</option>
<option value="8" class="states" id="states" data-id="1">Achimota</option>
<option value="9" class="states" id="states" data-id="2">Mampong</option>
<option value="10" class="states" id="states" data-id="1">Lapaz</option></select>           
</div>

这是我的jquery功能

<script>
$(document).ready(function(){

var s1 = $('#country');
var s2 = $('#states');

var dependantOptions = s2.find('option');
dependantOptions.css('visibility', 'hidden');

s1.on('change', function() {
dependantOptions.css('visibility', 'hidden');
s2.find("option[data-id='" + $(this).val() + "']").css('visibility',     'visible');
});

});
</script>

我无法弄清楚为什么javascript功能不起作用。 我希望很快能找到答案。感谢

1 个答案:

答案 0 :(得分:0)

您的选择列表只包含name="country"name="states"属性,并且没有相应的ID。你这样搜索:

var s1 = $('#country');
var s2 = $('#states');

因此,您应添加ID id="country"id="states"