Javascript 更改选择框
我正在尝试更改一个非常简单的下拉框的选择,以模拟用户单击某个选项时的情况 这是我正在使用的htmlJavascript 更改选择框,javascript,jquery,joomla,Javascript,Jquery,Joomla,我正在尝试更改一个非常简单的下拉框的选择,以模拟用户单击某个选项时的情况 这是我正在使用的html <select id="fwre-sel-country_id" name="search[country]"> <option value="" selected="selected">Any</option> <option value="262">USA</option> </select> 任何 美
<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>
$(文档).ready(函数(){
$('fwre-sel-country_id').val('262')
});
到目前为止,每当我刷新页面时,仍然会选择任意选项。为什么会这样
更新
更多背景我正在尝试更新一个joomla插件,它有一些预先编写的javascript脚本。所以我猜他们一定是在干扰我的提问。因此,我将把html/javascript内容发布到页面的这一部分(以确保我没有遗漏回答这个问题所需的任何信息)。我是一个完全的前端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>
属性地址
国家
任何
美国
setTimeout(函数(){
}, 500);
地区
setTimeout(函数(){
如果($('fwre-sel-country_id')){
$('fwre-sel-country_id')。addEvent('change',function(){
$('fwre-sel-region_id')。options.length=0;
如果(this.value=='(add value')返回;
var img=新元素('img'{
'id':'fwre wait country',
'style':'margin:0;padding:0;',
“src”:“/administrator/components/com_frealestatelight/assets/images/spinner.gif”
});
img.inject($('fwre-sel-region_id'),'after');
new Request.JSON({
url:“/properties?format=raw&task=loadDictionary”,
成功:功能(obj){
if($('fwre-wait-country'))$('fwre-wait-country').dispose();
如果(对象消息){
警报(obj.msg);
}else if(对象数据){
对于(变量i=0;i
城市
setTimeout(函数(){
if($('fwre-sel-region_id')){
$('fwre-sel-region_id')。addEvent('change',function(){
$('fwre-sel-city_id')。options.length=0;
如果(this.value=='(add value')返回;
var img=新元素('img'{
'id':'fwre等待区域',
'style':'margin:0;padding:0;',
“src”:“/administrator/components/com_frealestatelight/assets/images/spinner.gif”
});
img.inject($('fwre-sel-city_id'),'after');
new Request.JSON({
url:“/properties?format=raw&task=loadDictionary”,
成功:功能(obj){
if($('fwre-wait-region'))$('fwre-wait-region').dispose();
如果(对象消息){
警报(obj.msg);
}else if(对象数据){
对于(变量i=0;i
类型
任何
多联式出售
单件出售
类别
任何
公寓
家园
土地
牧场
价格范围
到
属性ID
搜寻
清楚的
//$(document).ready()块。
$(文档).ready(函数(){
$(“#fwre-sel-country_id option[value=“262”]”)prop('selected',true);
});
addEvent('domready',function(){
$('mod-fwrealestate-clear-button')。添加事件('click',函数(){
$$('.mod_frealestatelight_search input')。每个(函数(el){
如果(el.type=='text')el.value='';
});
});
});
试试这个:
$(document).ready(function () {
$('#fwre-sel-country_id option[value="262"]').prop('selected', true);
});
在这里它对我很有用:尝试删除“Selected=”Selected“你想在页面重新加载后保持选择吗?请注意Firefox的行为:它会记住所选的选项,并在刷新后保留它们,即使HTML或JS代码发生了更改。如果你看到一些奇怪的东西,试着在一个新的标签页中打开你的页面。我想他希望刷新后的选择是持久的:到目前为止,每当我刷新页面时,仍然会选择任何选项。为什么会这样?