Jquery动态设置下拉框selected --(2018 08/12-08/26周总结)

1、Jquery动态根据内容设置下拉框selected

需求就是根据下拉框的值动态的设置为selected,本以为很简单,网上一大推的方法,挨着尝试了之后却发现没有一个是有用的。网上的做法如下:

<select id="selectID ">
    <option>选择A</option>
    <option>选择B</option>
    <option>选择C</option>
</select>
// 方法一:
$("#selectID option[text='选择B']").attr("selected", "selected");
// 方法二:
$("#selectID ").find("option[text='选择B']").attr("selected",true);
// 方法三:也有人说高版本的jquery应该写成下面的样子
$("#selectID option[text='选择B']").prop("selected", true);

不管是用什么方法都不起作用,继续查找更多资料后上面这些方法在jquery低于1.4.2的版本(含)中有效,在更高版本中无效!!!

注意!!!上面的方法均不起作用,有效的方法如下:

解决一:精确匹配,选择文本与所给字符串完全一样的option。
$('#selectID option').filter(function(){return $(this).text()=="选择B";}).attr("selected",true);  
解决二:子串匹配,选择文本包含所给字符串的option。
$("#selectID option:contains('选择B')").attr('selected', true);  

2、struts2的action中的方法重复执行的原因

struts2中使用json插件(struts2-json-plugin)执行ajax处理时,如果方法名是get方法的时候,方法会莫名其妙的执行两次。
各种debug都找出原因在哪里,差点以为自己写的代码中邪了。又是继续百度之后,找到的问题的原因

原因:struts2 中JSON的原理是在ACTION中的get方法都会序列化,前面是get的方法只要没指定不序列化,都会在序列化时再执行一次。

解决方法:

1、Action中的业务方法前不要以get开头 (属性的get set 除外)
2、用@JSON(serialize=false)指定方法不序列化 (此办法没有亲自实现,仅供参考)

没有尝试添加注解的方式解决问题(因为改方法名更方便,并且get开头的方法名也不规范),所以以后在给方法起名字的时候,还是要十分注意,不要造成不必要的麻烦。

3、获取本机的ip地址(排除虚拟机等ip)

又是在网上查找资料遇到很多坑,很多Java获取本机ip地址的方法要么是根本获取不到,要么是获取的有问题。
网上常见的方法如下

InetAddress.getLocalHost().getHostAddress() 

但是如果电脑里面有Lan,WIFI,蓝牙热点,虚拟机网卡,即存在很多的网络接口(network interfaces),每个网络接口就包含一个IP地址,并不是所有的IP地址能被外部或局域网访问,比如说虚拟机网卡地址等等。上面获取到的ip就会有误。

下面是正确的获取ip地址的方法,亲测无误:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class getRealIp {
    public static void main(String[] args) {
        try {
            // 正确的IP拿法
            System.out.println("get LocalHost LAN Address : " + getLocalHostLANAddress().getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    // 正确的IP拿法,即优先拿site-local地址
    private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
        try {
            InetAddress candidateAddress = null;
            // 遍历所有的网络接口
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
                // 在所有的接口下再遍历IP
                for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
                    InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
                    if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
                        if (inetAddr.isSiteLocalAddress()) {
                            // 如果是site-local地址,就是它了
                            return inetAddr;
                        } else if (candidateAddress == null) {
                            // site-local类型的地址未被发现,先记录候选地址
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                return candidateAddress;
            }
            // 如果没有发现 non-loopback地址.只能用最次选的方案
            InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
            if (jdkSuppliedAddress == null) {
                throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
            }
            return jdkSuppliedAddress;
        } catch (Exception e) {
            UnknownHostException unknownHostException = new UnknownHostException(
                    "Failed to determine LAN address: " + e);
            unknownHostException.initCause(e);
            throw unknownHostException;
        }
    }
}

参考博客地址:https://blog.csdn.net/u011809209/article/details/77236602

4、获得userAgent(用户代理)的方法

通过userAgent可以判断用户当前操作的是桌面端设备还是移动设备,可以根据不同的设备进行适配。
js获取的方法:

var userAgent = navigator.userAgent

java后台写法:request为HttpServletRequest

String userAgent = request.getHeader("User-Agent");

5、css实现两端对齐的3种方法

本人是一个css渣,就不在这里班门弄斧了,给个传送门,可以参考这位大神的讲解,文末也有移动端文本两端对齐示例,可以说是非常好的学习资料了。
传送门:css实现两端对齐的3种方法

原文地址:https://www.cnblogs.com/ghq120/p/9537596.html

时间: 2024-08-01 20:42:21

Jquery动态设置下拉框selected --(2018 08/12-08/26周总结)的相关文章

jquery 根据后台传过来的值动态设置下拉框、单选框选中

jquery  根据后台传过来的值动态设置下拉框.单选框选中 1 $(function(){ 2 var sex=$("#sex").val(); 3 var marriageStatus=$("#marriageStatus").val(); 4 var education=$("#education").val(); 5 if(!isnull(sex)){ 6 //$("input:radio[name='sex'][value=&

jquery设置下拉框selected不起作用

在js中设置下拉框被选中: 最初写法: //移出selected $("#selected option").removeAttr("selected"); //将value值为value的设为selected $("#selected").find("option[value=]+value+"]").attr("selected",true); 来回切换几次后发现selected不起作用了.

jquery怎么根据后台传过来的值动态设置下拉框、单选框选中

$(function(){ var sex=$("#sex").val(); var marriageStatus=$("#marriageStatus").val(); var education=$("#education").val(); if(!isnull(sex)){ $("input:radio[name='sex'][value="+sex+"]").attr('checked','true

jQuery之双下拉框

双下拉框要实现的效果,实际上就是左边下拉选择框里的内容,可以添加到右边,而右边同理.写了个简单的例子,来说明一下. 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <!DOCTYPEhtml> <html> <head> <title>jq

jQuery操作select下拉框的text值和value值的方法

1.jquery获取当前选中select的text值 var checkText=$("#slc1").find("option:selected").text(); 2.jquery获取当前选中select的value值 var checkValue=$("#slc1").val(); 3.jquery获取当前选中select的索引值 var index=$("#slc1 ").get(0).selectedIndex; 4

JS/JQuery操作select下拉框

一.js 操作select 下拉框 var selObj = 下拉框对象 1. 移除所有项:selObj.options.length = 0; 2. 移除下拉框中的一项:selObj.options.remove(index); “index”为下拉框选项的索引值,若0索引项移出(自上而下),那么1索引项的索引会变为0,后面的索引依次向前推进 也可利用循环,移除所有项: var length = selObj.options.length; for(var i=length-1;i>=0;i-

Jquery EasyUI环境下设置下拉框选中指定选项

前提: 要求点击某个按钮就将所有的下拉框都设置为选中第一个选项,因此,指定索引是最好的做法 尝试过的做法: html代码如下(easyui 的写法) <select class="easyui-combobox"  name="query_1" id="query_1"> <option value="test1">测试1</option> <option value="te

js设置下拉框选中后change事件无效解决

下拉框部分代码: <select id="bigType"> <option value="">请选择</option> <option value="1">xiamen</option> <option value="2">beijing</option> </select> <select id="smallTy

jquery操作select下拉框的多种方法(选中,取值,赋值等)

jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text3. var checkValue=$("#select