首页 > 代码库 > JQuery 设置checkbox select radio 为“只读”
JQuery 设置checkbox select radio 为“只读”
JQuery 设置checkbox select radio 为“只读”,通过测试知道使用HTML标签的 text 和 textarea 、button 可以设置readonly="readonly"属性。
但是checkbox 、select、 radio 这三个控件使用起来不能使用readonly="readonly"属性,只有 disabled="disabled"可以使用。
这样就出现问题了。又想不让修改,又想在后台获取值。如果使用disabled属性,但是发现(string value = http://www.mamicode.com/Request.Form["XX"];)值为空。
不想修改后台代码,最后在和博哥的讨论之下,终于想出了下面的方法。
[html] view plaincopyprint?
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src=http://www.mamicode.com/"Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- function setSCR_ReadOnly() {
- var items = $("input[type=‘checkbox‘]")
- var html = ""
- for (var i = 0; i < items.length; i++) {
- items[i].disabled = true;
- html += "<input type=‘text‘ name=‘" + items[i].id + "‘ value=http://www.mamicode.com/‘" + items[i].value + "‘>";
- }
- var items = $("select");
- for (var i = 0; i < items.length; i++) {
- html += "<input type=‘text‘ name=‘" + items[i].id + "‘ value=http://www.mamicode.com/‘" + items[i].value + "‘>";
- items[i].disabled = true;
- }
- var items = $(":radio");
- for (var i = 0; i < items.length; i++) {
- html += "<input type=‘text‘ name=‘" + items[i].id + "‘ value=http://www.mamicode.com/‘" + items[i].value + "‘>";
- items[i].disabled = true;
- }
- $("#divhtml").html(html);
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="text" readonly="readonly" />
- <input type="button" value=http://www.mamicode.com/"不可用" onclick="setSCR_ReadOnly()" />
- <input type="checkbox" id="r" checked="checked" runat="server" value=http://www.mamicode.com/"cboVAlue" />
- <select id="sel" name="sel">
- <option value=http://www.mamicode.com/"请选择">请选择</option>
- <option value=http://www.mamicode.com/"1">1</option>
- </select>
- <select id="Select1" name="Select1">
- <option value=http://www.mamicode.com/"请选择">请选择</option>
- <option value=http://www.mamicode.com/"1" selected="selected">1</option>
- </select>
- <input id="rdo" name="rdo" type="radio" value=http://www.mamicode.com/"1" checked="checked" />
- <asp:Button runat="server" Text="Button" onclick="Unnamed1_Click" />
- <div style="display: none" id="divhtml">
- </div>
- </div>
- </form>
- </body>
- </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="http://www.mamicode.com/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> function setSCR_ReadOnly() { var items = $("input[type=‘checkbox‘]") var html = "" for (var i = 0; i < items.length; i++) { items[i].disabled = true; html += "<input type=‘text‘ name=‘" + items[i].id + "‘ value=http://www.mamicode.com/‘" + items[i].value + "‘>"; } var items = $("select"); for (var i = 0; i < items.length; i++) { html += "<input type=‘text‘ name=‘" + items[i].id + "‘ value=http://www.mamicode.com/‘" + items[i].value + "‘>"; items[i].disabled = true; } var items = $(":radio"); for (var i = 0; i < items.length; i++) { html += "<input type=‘text‘ name=‘" + items[i].id + "‘ value=http://www.mamicode.com/‘" + items[i].value + "‘>"; items[i].disabled = true; } $("#divhtml").html(html); } </script></head><body> <form id="form1" runat="server"> <div> <input type="text" readonly="readonly" /> <input type="button" value="http://www.mamicode.com/不可用" onclick="setSCR_ReadOnly()" /> <input type="checkbox" id="r" checked="checked" runat="server" value="http://www.mamicode.com/cboVAlue" /> <select id="sel" name="sel"> <option value="http://www.mamicode.com/请选择">请选择</option> <option value="http://www.mamicode.com/1">1</option> </select> <select id="Select1" name="Select1"> <option value="http://www.mamicode.com/请选择">请选择</option> <option value="http://www.mamicode.com/1" selected="selected">1</option> </select> <input id="rdo" name="rdo" type="radio" value="http://www.mamicode.com/1" checked="checked" /> <asp:Button runat="server" Text="Button" onclick="Unnamed1_Click" /> <div style="display: none" id="divhtml"> </div> </div> </form></body></html>将这些控件设置为disabled以后,发现这些控件就可以获取值。
[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Unnamed1_Click(object sender, EventArgs e)
- {
- string value = Request.Form["r"];
- string q = Request.Form["sel"];
- string s = Request.Form["Select1"];
- string w = Request.Form["rdo"];
- }
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。