Jquery動態設定下拉框selected --(2018 08/12-08/26周總結)

來源:互聯網
上載者:User

標籤:匹配   ssi   element   rgs   虛擬機器   代碼   序列化   null   imp   

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;        }    }}

參考部落格地址: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周總結)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.