我有这样的网址:
index.php/home/lista_modelo_ajax?id="+$('#colecoes').val(),
问题是:$('#colecoes').val()
始终是选择的第一项,而不是我选择的项目。
有什么想法吗?
提前致谢。
修改
<script>
$(function() {
$( "#modelos" ).autocomplete({
source: "<?php echo base_url();?>index.php/home/lista_modelo_ajax?id="+$('#colecoes').val(),
minLength: 2
});
});
</script>
<label for="colecoes">Coleções</label>
<select name="colecoes" id="colecoes">
<option value="16">-</option>
<?php foreach($lista_colecao as $colecao){?>
<option value="<?php echo $colecao->idColecao;?>"><?php echo $colecao->nomeColecao;?></option>
<?php }?>
</select>
<label for="modelos">Modelo</label>
<input type="text" name="modelos" id="modelos" />
所有php都运行正常,在options对象的值上生成ID,问题仅在于JS
答案 0 :(得分:2)
这取决于您编写此代码的位置。如果您直接在准备好的文档中将其写入,那么此代码执行时将是正常的,即所选值。另一方面,如果您在下拉列表的.change事件中编写它,您将获得实际值:
$('#colecoes').change(function() {
var id = $(this).val(); // now you will get the actual selected value
$.ajax({
url: 'index.php/home/lista_modelo_ajax',
data: { id: id },
success: function(result) {
...
}
});
});
答案 1 :(得分:0)
只是做:
$('#colecoes').val(); //assuming `colecoes` is the id of the select
onchange示例:
$('#colecoes').change(function(){
alert(this.value); //alerts the selected option
});