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

Posted ghq120

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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种方法

以上是关于Jquery动态设置下拉框selected --(2018 08/12-08/26周总结)的主要内容,如果未能解决你的问题,请参考以下文章

JS与Jquery之动态添加下拉框select并级联改变事件

jquery给下拉框设置默认值

“jquery”中怎么让下拉框不能选择?

JQuery MiNiUI 多选多列下拉列表如何动态赋值。

jquery动态添加下拉框选项设置下拉框样式怎么做啊

jquery设置下拉框selected不起作用