0

现在在页面加载时,我需要选择美国作为我的默认值。

我正在编写的代码是:

$(document).ready(function(){
    if ($("input[name='contactAddress.id']").val() == "") {

        $(contactAddress.country).find('option').filter(function() {
            return this.text == 'United States';
        }).attr('selected','selected');
        $(contactAddress.country).val('United States');
    }
});

下拉代码:

 <tr>
            <th><s:text name="contactAddress.country"/>:</th>
            <td>
                <v:list var="countries" action="country" namespace="/"/>
                <s:select name="contactAddress.country"
                    value="contactAddress.country.id"
                    headerKey="" 
                    headerValue="-- Please Select --" 
                    list="countries"
                    listKey="id" 
                    listValue="name"/>                                                      
                <s:fielderror><s:param>contactAddress.country</s:param></s:fielderror>              
            </td>
            <th><s:text name="contactAddress.zipPostalCode"/>:</th>
            <td><s:textfield name="contactAddress.zipPostalCode" /></td>
        </tr>

但这不起作用。我一直在谷歌搜索并为此尝试了许多不同的语法,但没有任何效果。我在哪里错了。请帮忙

4

2 回答 2

1

你试过了吗

$("select[name='contactAddress.country']")

代替

$(contactAddress.country)

你也知道只是写作

$(document).ready(function(){
    if ($("input[name='contactAddress.id']").val() == "") {
       $("select[name='contactAddress.country']").val('United States');           
    }
});

处理下拉所选项目的更改

笔记 :

jquery 不理解这个

$('#contactAddress.country') 

正在寻找具有“国家”类的“contactAddress”的孩子

于 2013-09-30T18:16:54.977 回答
0

这应该工作

$(document).ready(function(){
    if ($("input[name='contactAddress.id']").val() == "") {

    var element = document.getElementById('#contactAddress.country');
    element.value = 'United States';

    }
});

您可能不需要 ID 名称中的 #

于 2013-09-30T18:20:37.630 回答