Jquery操作服务器控件 text和val

来源:互联网 发布:程序员最讨厌的两件事 编辑:程序博客网 时间:2024/06/03 21:23
 
 <script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>    <script type="text/javascript" language="javascript">        $(function () {            $("#Checkbox2").click(function () {                var str = null;                if ($("#Checkbox2").is(":checked")) {                    str = "true";                }                else {                    str = "false";                }                $("#Text1").val(str);//单行文本用val,不能用text                $("#TextBox1").val(str); //或者$("input[id*=TextBox1]").val(str);                $("#CheckBox1").attr("checked", true);                $("#lbName").html(str);                alert(str);            });        });    </script>


 

 

 <asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    <input id="Checkbox2" type="checkbox" /><input id="Text1" type="text" />    <asp:Label ID="lbName" runat="server" Text="sd"></asp:Label>


 

 

text()方法是取得所有匹配元素的内容。结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。获取span,div ,p之类才用text()或html()方法,服务器Label也用html。

例子:

<p>Paraparagraph</p>

$("p").text();     将得到:Paraparagraph

单行文本<input type="text" ...>不能用text()方法获得值,必须用val()方法,服务器控件TextBox也用val()。

val()方法是获得第一个匹配元素的当前值。包括select。如果多选,将返回一个数组,其包含所选的值。

例:

<input type="text" value="some text"/>

$("input").val();     将得到:some text

 

 

 

操作dropdownList

 alert($("#DropDownList1 option:selected").text());//选中项的文本
alert($("#DropDownList1").get(0).selectedIndex);//选中项的索引