解决方案 »

  1.   

    做了个演示代码<input type="text" id="address" value="请输入邮箱地址" /><br>
    <input type="text" id="password" value="请输入邮箱密码" /><br>
    <input type="button" value="登录" /><br>
    <br><br>
    <input type="button" id="btn1" value="使单选下拉框的*选择3号*被选中" />
    <input type="button" id="btn2" value="使多选下拉框选中的*选择2号*和*选择4号*被选中" /><br>
    <input type="button" id="btn3" value="使多选框的*多选2*和*多选4*被选中" />
    <input type="button" id="btn4" value="使单选框的*单选2*被选中" /><br>
    <input type="button" id="btn5" value="打印已经被选中的值" /><br>
    <br/>
    <select id="single">
        <option>选择1号</option>
        <option>选择2号</option>
        <option>选择3号</option>
    </select><select id="multiple" multiple="multiple" style="height:120px;">
        <option selected="selected">选择1号</option>
        <option>选择2号</option>
        <option>选择3号</option>
        <option>选择4号</option>
        <option selected="selected">选择5号</option>
    </select>
    <br/><br/>
    <input type="checkbox" name="c" value="check1" /> 多选1
    <input type="checkbox" name="c" value="check2" /> 多选2
    <input type="checkbox" name="c" value="check3" /> 多选3
    <input type="checkbox" name="c" value="check4" /> 多选4
    <br/>
    <input type="radio" name="r" value="radio1" /> 单选1
    <input type="radio" name="r" value="radio2" /> 单选2
    <input type="radio" name="r" value="radio3" /> 单选3
    $(document).ready(function(){
        toggleInput("#address", "请输入邮箱地址");
        toggleInput("#password", "请输入邮箱密码");
        $("#btn1").click(function(){
            setSelection("#single", "选择3号");
        });
        $("#btn2").click(function(){
            setSelection("#multiple", ["选择2号", "选择4号"]);
        });
        $("#btn3").click(function(){
            setSelection("input[name='c']", ["check2", "check4"]);
        });
        $("#btn4").click(function(){
            setChecked("input[name='r'][value='radio2']", true);
        });
        $("#btn5").click(function(){
            $("input:text, option:selected, input:checked").map(function(){
                alert($(this).val());
            })
        });
     });function toggleInput(el, msg){
        $(el).focus(function(){
            if($(this).val() == msg){
                $(this).val("");
            }
        }).blur(function(){
            if($(this).val() == ""){
                $(this).val(msg);
            }
        });
    }function setSelection(el, selected){
        $(el).val(selected);
    }function setChecked(el, checked){
        $(el).prop("checked", checked);
    }