0

我正在尝试更改一个非常简单的下拉框的选定项,以模拟用户单击选项时的情况。

这是我正在使用的 html

<select id="fwre-sel-country_id" name="search[country]">
    <option value="" selected="selected">Any</option>
    <option value="262">USA</option>
</select>

这是我到目前为止写的Jquery

<script type="text/javascript">
$( document ).ready(function() {
    $('#fwre-sel-country_id').val('262')
});
</script>

到目前为止,每当我刷新页面 Any 仍然是被选中的选项。为什么会这样?

更新更多背景 我正在尝试更新一个 joomla 插件,它有一些预先编写的 javascript 脚本。所以我的猜测是他们必须以某种方式干扰我的查询。所以我只是将 html/javascript reavlent 发布到页面的部分(以确保我没有遗漏任何需要的信息来回答这个问题)。我是前端 Web 开发的完整初学者,对于文本转储,我提前道歉......这里是

<div class="mod_fwrealestatelight_search">
    <form action="/properties/search" method="post">
        Property Address<br>
        <input class="inputbox" type="text" name="search[location]" value=""><br>
        Country<br>
        <div id="el-country_id686736516">
    <select id="fwre-sel-country_id" name="search[country]">
    <option value="" selected="selected">Any</option>
    <option value="262">USA</option>
</select>
</div>
<script type="text/javascript">
setTimeout(function() {
}, 500);
</script>
<br>
        Region<br>
        <div id="el-region_id558729726">
    <select id="fwre-sel-region_id" name="search[region]">
</select>
</div>
<script type="text/javascript">
setTimeout(function() {
    if ($('fwre-sel-country_id')) {
        $('fwre-sel-country_id').addEvent('change', function() {
            $('fwre-sel-region_id').options.length = 0;
            if (this.value == '(add-value)') return;
            var img = new Element('img', {
                'id': 'fwre-wait-country',
                'style' : 'margin:0;padding:0;',
                'src': '/administrator/components/com_fwrealestatelight/assets/images/spinner.gif'
            });
            img.inject($('fwre-sel-region_id'), 'after');
             new Request.JSON({
                url: '/properties?format=raw&task=loadDictionary',
                onSuccess: function(obj) {
                    if ($('fwre-wait-country')) $('fwre-wait-country').dispose();
                    if (obj.msg) {
                        alert(obj.msg);
                    } else if (obj.data) {
                        for (var i = 0; i < obj.data.length; i++) {
                            var op = new Element('option', {
                                'value': obj.data[i].id
                            });
                            op.inject($('fwre-sel-region_id'));
                            op.innerHTML = obj.data[i].name;
                        }
                    }
                    $('fwre-sel-region_id').fireEvent('change');
                }
            }).get({'parent_id':$('fwre-sel-country_id').value,'dname':'region_id'});
        });
    }
}, 500);
</script>
<br>
        City<br>
        <div id="el-city_id1146655005">
    <select id="fwre-sel-city_id" name="search[city]">
</select>
</div>
<script type="text/javascript">
setTimeout(function() {
    if ($('fwre-sel-region_id')) {
        $('fwre-sel-region_id').addEvent('change', function() {
            $('fwre-sel-city_id').options.length = 0;
            if (this.value == '(add-value)') return;
            var img = new Element('img', {
                'id': 'fwre-wait-region',
                'style' : 'margin:0;padding:0;',
                'src': '/administrator/components/com_fwrealestatelight/assets/images/spinner.gif'
            });
            img.inject($('fwre-sel-city_id'), 'after');
             new Request.JSON({
                url: '/properties?format=raw&task=loadDictionary',
                onSuccess: function(obj) {
                    if ($('fwre-wait-region')) $('fwre-wait-region').dispose();
                    if (obj.msg) {
                        alert(obj.msg);
                    } else if (obj.data) {
                        for (var i = 0; i < obj.data.length; i++) {
                            var op = new Element('option', {
                                'value': obj.data[i].id
                            });
                            op.inject($('fwre-sel-city_id'));
                            op.innerHTML = obj.data[i].name;
                        }
                    }
                    $('fwre-sel-city_id').fireEvent('change');
                }
            }).get({'parent_id':$('fwre-sel-region_id').value,'dname':'city_id'});
        });
    }
}, 500);
</script>
<br>
        Type<br>
        <select id="searchproperty_type" name="search[property_type]" class="inputbox">
    <option value="" selected="selected">Any</option>
    <option value="13">Mutli-Type For Sale</option>
    <option value="12">Single-Type For Sale</option>
</select>
<br>
        Category<br>
        <select id="searchcategory" name="search[category]" class="inputbox">
    <option value="" selected="selected">Any</option>
    <option value="7">Condos</option>
    <option value="4">Homes</option>
    <option value="5">Land</option>
    <option value="6">Ranches</option>
</select>
<br>
        Price Range<br>
        <input class="inputbox" name="search[price_from]" size="6" value="">
        to      <input class="inputbox" name="search[price_to]" size="6" value=""><br>
        Property ID<br>
        <input class="inputbox" name="search[id]" size="6" value=""><br>

        <input type="hidden" name="limitstart" value="0">
        <input type="hidden" name="fwrealestate_update_search" value="1">
        <button type="submit">Search</button>
        <button type="button" id="mod-fwrealestate-clear-button">Clear</button>
    </form>
</div>
<script type="text/javascript">
// A $( document ).ready() block.
$(document).ready(function () {
    $('#fwre-sel-country_id option[value="262"]').prop('selected', true);
});
</script>
<script type="text/javascript">
window.addEvent('domready', function() {
    $('mod-fwrealestate-clear-button').addEvent('click', function() {
        $$('.mod_fwrealestatelight_search input').each(function(el) {
            if (el.type == 'text') el.value = '';
        });
    });
});
</script>
4

1 回答 1

1

尝试这个:

$(document).ready(function () {
    $('#fwre-sel-country_id option[value="262"]').prop('selected', true);
});
于 2013-06-02T08:15:06.507 回答