jQuery来了--text(),html(),val(),attr(),捕获内容/属性,设置内容/属性,回调函数

捕获

获得内容–text(),html()和val()

三个用于DOM操作的jQuery方法:

  • text():设置或返回所选元素的文本内容
  • html():设置或返回所选元素的内容(包括HTML标记)
  • val():设置或返回表单字段的值

例1.
通过text()和html()方法获得内容:

<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    alert("Text: " + $("#test").text());
  });
  $("#btn2").click(function(){
    alert("HTML: " + $("#test").html());
  });
});
</script>
<p id="test">这是段落中的 <b>粗体</b> 文本。</p>
<button id="btn1">显示文本</button>
<button id="btn2">显示 HTML</button>

例2.
通过val()方法获得输入字段的值:

<script>
$(document).ready(function(){
  $("button").click(function(){
    alert("值为: " + $("#test").val());
  });
});
</script>

<p>名称: <input type="text" id="test" value="菜鸟教程"</p>
<button>显示值</button>

获取属性–attr()

作用:用于获取属性值

例.
如何获得链接中href属性的值:

<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("#runoob").attr("href"));
  });
});
</script>

<p><a href="http://www.runoob.com" id="runoob">菜鸟教程</a></p>
<button>显示 href 属性的值</button>

设置

设置内容–text(),html()和val()

  • text():设置或返回所选元素的文本内容
  • html():设置或返回所选元素的内容(包括HTML标记)
  • val():设置或返回表单字段的值

例.

<script>
   $(document).ready(function(){
      $("#btn1").click(function(){
         $("#test1").text("Hello Nico");
      });
      $("#btn2").click(function(){
         $("#test2").html("<h1>Hello Nico</h1>");
      });
      $("#btn3").click(function(){
         $("#test3").val("Haha");
      });
   });
</script>

<p id="test1">这是一个段落。</p>
<p id="test2">这是另外一个段落。</p>
<p>输入框: <input type="text" id="test3"></p>
<button id="btn1">设置文本</button>
<button id="btn2">设置 HTML</button>
<button id="btn3">设置值</button>

text()、html() 和 val() 的回调函数

回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串

例.

<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    $("#test1").text(function(i,origText){
      return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")"; 
    });
  });

  $("#btn2").click(function(){
    $("#test2").html(function(i,origText){
      return "旧 html: " + origText + " 新 html: Hello <b>world!</b> (index: " + i + ")"; 
    });
  });
});
</script>

<p id="test1">gg</p>
<p id="test2">hh</p>
<button id="btn1">显示 新/旧 文本</button>
<button id="btn2">显示 新/旧 HTML</button>

设置属性 - attr()

作用:设置/改变属性值

例.

<script>
$(document).ready(function(){
  $("button").click(function(){
     $("#th").attr("name"," Nico");
      alert($("#th").attr("name"));
  });
});
</script>

<p><a href="#" id="th" name="hh">Hello</a></p>
<button>修改 href 值</button>

attr() 方法也允许您同时设置多个属性

例.
设置多个属性:

<script>
 $(document).ready(function(){
     $("button").click(function(){
        $("#ha").attr({
        "name":"Nico",    //修改属性name
        "title":"qq"      //设置属性title
        });
     $("#ha").text($("#ha").attr("title"));// 通过修改的 title 值来修改链接名称
     });
  });
</script>

<p><a href="#" id="ha">Hello Nico</a></p>
<button>change</button>

attr() 的回调函数

例.

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#ha").attr("href", function(i, origValue){
            return origValue + "/ditu";
            /*或return "http://www.sohu.com";*/
        });
        /*$("#ha").attr("href","http://www.baidu.com");*/
    });
});
</script>

<p><a href="http://www.baidu.com" id="ha">Hello Nico</a></p>

<button>修改 href 值</button>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> <%@ include file="/commons/taglibs.jsp"%> <%@ include file="/commons/msg.jsp"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>${applicationScope['System.ConsoleWebName']}</title> <link href="${ctx}/console/styles/common/main.css" rel="stylesheet" type="text/css" /> <link href="${ctx}/console/styles/extremetable/extremecomponents.css" rel="stylesheet" type="text/css"> <link href="${ctx}/console/styles/validation/validation_style.css" rel="stylesheet" type="text/css" /> <script src="${ctx}/console/scripts/jquery/${applicationScope['System.JQuery.JS']}" type="text/javascript"></script> <script src="${ctx}/console/scripts/extremetable/extremecomponents.js" type="text/javascript"></script> <script type="text/javascript" src="${ctx}/console/scripts/validation/validation.js"></script> <script type="text/javascript"> // 查询方法设置首页 function setPage() { $("input[name='ec_p']").val(1); } function submitValue() { const checkedBoxes = $("input[name='sub']:checked"); if (checkedBoxes.length === 0) { alert("请至少选择一个网段"); console.log("选中的网段: 无"); return false; } const selectedSubnets = checkedBoxes.map(function() { return $(this).val(); }).get(); console.log("选中的网段:", selectedSubnets); // 现在变量名正确 return true; } </script> </head> <body> <div class="wrap"> <form action="${ctx}/console/admin/AdminManage.do?method=doSubList" method="post" id="presForm" name="presForm"> <!-- 隐藏域 开始 --> <cpm:input type="hidden" id="search" name="search" value="role" /> <!-- 隐藏域 结束 --> <!-- 导航 开始 --> <%-- <cpm:position pos="分级管理员管理-角色管理"/> --%> <cpm:br /> <!-- 导航 结束 --> <!-- 功能描述 开始 --> <cpm:description content="管理员角色列表" /> <cpm:br /> <!-- 功能描述 结束 --> <!-- 页面查询 开始 --> <cpm:wrap> <table width="100%" border="0" align="left" cellspacing="1" cellpadding="0" class="mainForm_z"> <tr> <td class="divLeft"><span class="spanTxt">网段</span></td> <td style="padding-left: 10px;"><cpm:input type="text" id="keyword" name="keyword" value="${keyword}" /></td> </tr> <tr> <td colspan="4" style="padding-left: 10px; padding-top: 5px; padding-bottom: 5px;"> <cpm:btn category="mini" id="searchBtn" name="searchBtn" value="查询" forceSubmit="true" onclick="javascript: setPage();" />   </td> </tr> </table> </cpm:wrap> <cpm:br /> <cpm:wrap> <ec:table items="subList" var="item" action="${ctx}/console/admin/AdminManage.do?method=doSubList&keyword=${keyword}" view="footerView" form="presForm" filterable="false" cellspacing="0"> <ec:row highlightRow="true"> <ec:column property="rowCount" cell="rowCount" title="行号" width="5%" sortable="false" style="text-align: center;" /> <ec:column property="subnetAddress" title="网段" width="25%" sortable="false" style="padding-left: 5px;" /> <ec:column property="subnetAddress" title="选择" width="5%" style="text-align: center;" > <cpm:input type="checkbox" name="sub" value="${item.subnetAddress}"/> </ec:column> </ec:row> </ec:table> </cpm:wrap> <!-- 角色列表 结束 --> <cpm:br height="20px" /> </form> </div> </body> </html><%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> <%@ include file="/commons/taglibs.jsp"%> <%@ include file="/commons/msg.jsp"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>${applicationScope['System.ConsoleWebName']}</title> <link href="${ctx}/console/styles/common/main.css" rel="stylesheet" type="text/css" /> <link href="${ctx}/console/styles/validation/validation_style.css" rel="stylesheet" type="text/css" /> <script src="${ctx}/console/scripts/jquery/${applicationScope['System.JQuery.JS']}" type="text/javascript"></script> <script src="${ctx}/console/scripts/validation/validation.js" type="text/javascript"></script> <script type="text/javascript" src="${ctx}/console/scripts/validation/validation-ui-utils.js"></script> <script src="${ctx}/console/scripts/sha1/sha1.js" type="text/javascript"></script> <link href="${ctx}/console/styles/extremetable/extremecomponents.css" rel="stylesheet" type="text/css"> <link href="${ctx}/console/styles/jqueryui/jquery-ui-1.7.1.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="${ctx}/console/scripts/extremetable/extremecomponents.js"></script> <script type="text/javascript" src="${ctx}/console/scripts/common/rafdialogutils.js"></script> <script type="text/javascript" src="${ctx}/console/scripts/jquery/jquery-ui-dialog-1.7.1.min.js"></script> <script type="text/javascript" src="${ctx}/console/scripts/validation/validation.js"></script> <script type="text/javascript"> function check() { var objform = new web_validator(); objform.add("adminName", "管理员姓名不能为空,长度必须大于等于2,小于等于10,且不能包含特殊符号!", "[notnull]&&[specialchar]&&[length,>=,2]&&[length,<=,10]"); objform.add("loginName", "管理员帐号不能为空,长度必须大于等于2,小于等于20,且不能包含特殊符号!", "[notnull]&&[specialchar]&&[length,>=,2]&&[length,<=,20]"); objform.add("commonPassword", "管理员登录口令不能为空,密码长度须在${adminPwdMinLength}~${adminPwdMaxLength}位之间,由英文字母a~z (区分大小写)、数字0~9、特殊字符至少两种组合组成,且不可与帐号相同!", "[notnull]&&([length,>=,${adminPwdMinLength}]&&[length,<=,${adminPwdMaxLength}])"); objform.add("confirmpassword", "管理员确认口令不能为空或您两次输入的登录口令不一致,请重新输入!", "[notnull]&&[equaltext,commonPassword]"); objform.add("roleId", "请选择管理员角色!", "[selected]"); var result = objform.check(); if (!result) { return false; } var confirm = false; var loginName = jQuery("#loginName").val(); if (/.*[\u4e00-\u9fa5]+.*$/.test(loginName)) { alert("管理员姓名不能为空,长度必须大于等于2,小于等于10,且不能包含特殊符号!"); return false; } var commonPassword = jQuery("#commonPassword").val(); if (commonPassword == loginName) { alert("管理员登录口令不可与帐号相同!"); return false; } if (/^.*?[\d]+.*$/.test(commonPassword) && /^.*?[A-Za-z].*$/.test(commonPassword)) confirm = true; else if (/^.*?[\d]+.*$/.test(commonPassword) && /^.*?[!@#$-].*$/.test(commonPassword)) confirm = true; else if (/^.*?[A-Za-z].*$/.test(commonPassword) && /^.*?[!@#$-].*$/.test(commonPassword)) confirm = true; if (!confirm) { alert("管理员登录口令不能为空,口令长度${adminPwdMinLength}~${adminPwdMaxLength}位,由英文字母a~z (区分大小写)、数字0~9、特殊字符至少两种组合组成,且不可与帐号相同!"); return false; } var passwd = hex_sha1(jQuery("#commonPassword").val()); jQuery("#commonPassword").val(passwd); jQuery("#password").val(passwd); jQuery("#confirmpassword").val(passwd); return true; } function getCAInfo() { var validator = new web_validator(); validator.add("caServerIp", "CA服务地址不能为空或格式不正确!", "[notnull] && [ipaddress]", trimObj); validator.add("caServerPort", "CA服务地址端口不能为空且只能是0~65535之间的数字!", "[notnull] && [scale,>,0] && [scale,<=,65535]", trimObj); validator.add("caServerName", "CA服务名不能为空!", "[notnull]", trimObj); validator.add("raunicode", "RA唯一编码不能为空!", "[notnull] && [length,<=,10]", trimObj); var result = validator.check(); if (result) { var caServerIp = jQuery("#caServerIp").val(); var caServerPort = jQuery("#caServerPort").val(); var caServerName = jQuery("#caServerName").val(); var isSSL = jQuery("#isSSL").val(); var raunicode = jQuery("#raunicode").val(); var url = "${ctx}/console/credential/caConfig.do?method=loadCAInfo&caServerIp=" + caServerIp + "&caServerPort=" + caServerPort + "&caServerName=" + caServerName + "&isSSL=" + isSSL + "&raunicode=" + raunicode + "&random=" + Math.random() + ""; popupDialog({ title : "获取CA信息", content : url, width : 600, height : 400, modal : true, overlay : true, buttons : { "确认并保存" : function() { var frameName = $(this).attr("name"); var frameObj = eval(frameName); if (frameObj.submitData()) { alert("保存CA配置成功!"); window.location = "${ctx}/console/credential/caConfig.do?method=caConfig&random=" + Math.random() + ""; $(this).dialog("close"); } }, "关闭窗口" : function() { $(this).dialog("close"); } } }); } } function chooseDept() { var url = "${ctx}/console/admin/AdminManage.do?method=doSubList"; popupDialog({ title : "选择网段", content : url, width : 600, height : 700, modal : true, overlay : true, buttons : { "确认" : function() { var frameName = $(this).attr("name"); var frameObj = eval(frameName); if (frameObj.submitValue()) { // 收集所有选中的网段地址 var selectedSubnets = []; $("input[name='sub']:checked").each(function() { selectedSubnets.push($(this).val()); }); // 将选中的网段地址设置到目标输入框 $("#subnetAddress").val(selectedSubnets.join(", ")); // 关闭对话框 $(this).dialog("close"); } }, "关闭" : function() { $(this).dialog("close"); } } }); } </script> </head> <body> <div class="wrap"> <form action="${ctx}/console/admin/AdminManage.do?method=doAdminAdd" method="post" name="presForm" onsubmit="return check();"> <!-- 导航 开始 --> <%-- <cpm:position pos="分级管理员管理-管理员管理-创建管理员"/> --%> <cpm:br /> <!-- 导航 结束 --> <!-- 功能描述 开始 --> <cpm:description content="带红色“<font color=red>*</font>”为必填项" /> <cpm:br /> <!-- 功能描述 结束 --> <!-- Tab部分 开始 --> <cpm:staticTabs> <cpm:staticTab title="创建管理员" /> </cpm:staticTabs> <!-- Tab部分 结束 --> <!-- 角色信息 开始 --> <cpm:wrap> <table width="100%" border="0" align="left" cellspacing="1" cellpadding="0" class="mainForm"> <tr class="tr_title"> <td style="padding-left: 5px; font-size: 12px; font-weight: bold;" colspan="2">基本配置</td> </tr> <tr> <td class="left">管理员帐号</td> <td class="right"><cpm:input type="text" id="loginName" name="loginName" required="true" value="${user.loginName}" readonly="${!empty user ? true : false}" /></td> </tr> <tr> <td class="left">管理员姓名</td> <td class="right"><cpm:input type="text" id="adminName" name="adminName" required="true" value="${user.userName}" readonly="${!empty user ? true : false}" /></td> </tr> <tr> <td class="left">登录口令</td> <td class="right"><cpm:input type="password" id="commonPassword" name="commonPassword" required="true" /> <cpm:input type="hidden" id="password" name="password" /> <font color="blue">  ( 注:密码长度${adminPwdMinLength}~${adminPwdMaxLength}位,由英文字母a~z (区分大小写)、数字0~9、特殊字符至少两种组合组成,且不可与帐号相同!)</font></td> </tr> <tr> <td class="left">确认口令</td> <td class="right"><cpm:input type="password" id="confirmpassword" name="confirmpassword" required="true" /> <font color="blue">  ( 注:两次输入密码相同时才会更改密码)</font></td> </tr> <tr class="tr_title"> <td style="padding-left: 5px; font-size: 12px; font-weight: bold;" colspan="2">权限信息</td> </tr> <tr> <td class="left">所属角色</td> <td class="right"><select name="roleId" id="roleId" class="txt"> <c:if test="${empty adminRoles}"> <option value="">未授权可管理的管理员角色</option> </c:if> <c:if test="${!empty adminRoles}"> <c:forEach items="${adminRoles}" var="item"> <option value="${item.id}">${item.roleName}</option> </c:forEach> </c:if> </select>  <span style="color: red;">*</span></td> </tr> <tr> <td class="left">网段</td> <td class="right" colspan="2"><cpm:input type="text" name="subnetAddress" id="subnetAddress" readonly="true" onclick="chooseDept();" /> <font color="gray">请选择网段</font></td> </tr> </table> </cpm:wrap> <cpm:br /> <!-- 角色信息 结束 --> <!-- 操作部分 开始 --> <cpm:wrap> <cpm:btn category="submit" name="submitBtn" value="保存" /> <cpm:btn category="return" name="returnBtn" value="返回" onclick="javascript: window.location = '${ctx}/console/admin/AdminManage.do?method=doAdminList';" /> </cpm:wrap> <!-- 操作部分 结束 --> <cpm:br height="20px" /> </form> </div> </body> </html> 第二个jsp页面点击确认选不到网段信息该咋该
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值