jquery Dynamic Settings Dropdown box Selected--(2018 08/12-08/26 Week summary)

Source: Internet
Author: User

1, jquery dynamic based on Content Settings dropdown box selected

Demand is based on the value of the dropdown box dynamic settings for selected, this thought is very simple, online a big push method, next to try to find that no one is useful. The online approach is as follows:

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

No matter what method does not work, continue to find more information after the above methods in jquery less than 1.4.2 version (included) valid, in later version invalid!!!

Attention!!! The methods above do not work, and the effective method is as follows: Solve a: Exact match, select the text with the given string exactly the same option.
$('#selectID option').filter(function(){return $(this).text()=="选择B";}).attr("selected",true);  
Resolve two: Substring match, select the text containing the given string option.
$("#selectID option:contains('选择B')").attr('selected', true);  
2. The reason for repeated execution of methods in the Struts2 action

When using the JSON plug-in (Struts2-json-plugin) to perform AJAX processing in Struts2, the method executes two of times if the method name is a GET method.
All sorts of debug find out the reason where, almost thought oneself writes the code possessed. Again after the Baidu, to find the cause of the problem

Cause: The principle of JSON in STRUTS2 is that the Get method in the action is serialized, and the previous method of get is executed once at serialization, as long as no serialization is specified. Workaround:

1. Do not start with get before the business method in action (except for the property's get Set)
2, using @json (serialize=false) to specify the method is not serialized (this method is not implemented personally, for reference only)

There is no attempt to add annotations to solve the problem (because the method name is more convenient, and get at the beginning of the method name is not standard), so in the future when the name of the method, you should pay great attention, do not cause unnecessary trouble.

3. Get the IP address of the machine (excluding the virtual machine and other IP)

Again on the Internet to find data encountered a lot of pits, a lot of Java to get the native IP address of the method is either simply not get, or get the problem.
The following are common online methods

But if the computer has Lan,wifi, Bluetooth hotspot, virtual machine network card, that is, there are many network interfaces (networks interfaces), each network interface contains an IP address, not all IP addresses can be external or local area network access, such as virtual machine network card address and so on. The IP you acquired above will be wrong.

The following is the correct way to obtain an IP address, the pro-Test error:
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 {//correct I        P-Law System.out.println ("Get LocalHost LAN Address:" + getlocalhostlanaddress (). gethostaddress ());        } catch (Unknownhostexception e) {e.printstacktrace ();         }}//Correct IP fetch method, that is, priority to take site-local address private static inetaddress getlocalhostlanaddress () throws Unknownhostexception {            try {inetaddress candidateaddress = null; Traverse all network interfaces for (Enumeration ifaces = Networkinterface.getnetworkinterfaces (); ifaces.hasmoreelements ();)                {NetworkInterface iface = (networkinterface) ifaces.nextelement (); Traverse IP for (Enumeration Inetaddrs = Iface.getinetaddresses (), inetaddrs.hasmoreelements ();) on all interfaces {inetaddress InetAddr = (inetaddreSS) Inetaddrs.nextelement ();                            if (!inetaddr.isloopbackaddress ()) {//Excludes loopback type address if (inetaddr.issitelocaladdress ()) {                        If it is the site-local address, it is the return inetaddr;                            } else if (candidateaddress = = null) {///site-local type address not found, first record candidate address                        candidateaddress = InetAddr; }}}} if (candidateaddress! = null) {return cand            idateaddress;            }//If no Non-loopback address is found. Only the most selected scheme inetaddress jdksuppliedaddress = Inetaddress.getlocalhost (); if (jdksuppliedaddress = = null) {throw new Unknownhostexception ("The JDK Inetaddress.getlocalhos            T () method unexpectedly returned null. ");        return jdksuppliedaddress; } catch (Exception e) {Unknownhostexception unknownhostexception = new Unknownhostexception ("Failed to determine LAN address:" + e);            Unknownhostexception.initcause (e);        Throw unknownhostexception; }    }}

Reference Blog Address: 77236602

4, access to useragent (user agent) method

The useragent can be used to determine whether a user is currently operating on a desktop or mobile device, and can be adapted to a different device.
JS Gets the method:

var userAgent = navigator.userAgent

Java Spooler: Request for HttpServletRequest

String userAgent = request.getHeader("User-Agent");
5, CSS to achieve two-way alignment of 3 ways

I am a CSS slag, it is not here swim, to a portal, you can refer to the explanation of the great God, at the end of the text there are mobile texts at both ends of the alignment example, can be said to be very good learning materials.
Portal: 3 ways to align CSS with two sides

jquery Dynamic Settings Dropdown box Selected--(2018 08/12-08/26 Week summary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.