【问题标题】:The jQuery val() does not update its value on changejQuery val() 不会在更改时更新其值
【发布时间】:2014-03-24 05:11:16
【问题描述】:

我更改了#countrycode 值,但 $(#countrycode).val() 没有在“源”选项中刷新/更新,而是在警报中更新

$( document ).ready(function() {

    //List cities with autocompelte
    $( "#city" ).autocomplete(
    {
        source:"autocomplete.php?countrycode=" + $("#countrycode").val(),
        select: function (event, ui) {          

            alert($("#countrycode").val()); //Test the countrycode selector
     }
    });
});

警报显示更新的值,但源中使用的相同选择器(国家代码)没有,我做错了什么?

【问题讨论】:

  • 我解决了。源 url 第一次被呈现为字符串,这就是它没有在输入更改时更新的原因。所以解决方案是在source选项中传递一个匿名函数而不是字符串属性,并在source选项内部进行ajax调用。我会发布代码,但 stackoverflow 不允许我回答自己的问题。呜呜。

标签: jquery-ui autocomplete jquery-selectors refresh


【解决方案1】:

试试这个

$("#countrycode option:selected").prop("selected","false");
$("#countrycode option[value='".$("#countrycode").val()."']").prop("selected","true");

它将使选项被选中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多