• Js/jquery常用


    id属性不能有空格

    1. js判断checkebox是否被选中

    var ischecked = document.getElementById("xxx").checked  //返回false or true

    设置选上/不选:document.getElementById("xxx").checked=true/false

    Jquery

     判断选上:$('#isAgeSelected').attr('checked') // true//false

    选上:

    $('.myCheckbox').prop('checked', true) or
    $('.myCheckbox').attr('checked', true) or
    $(".myCheckBox").checked(true);

    不选改为false

    2. jquery 判断selecte的值

    <select name="selector" id="selector">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    </select>

    $('select[name=selector]').val()

    选中Option 1

    $('select[name=selector]').val("1");

    3. 往Array中存入对象,

    var regions = new Array();

    var ri = new Object();
    ri.id = "";
    ri.name = "";
    regions.push(ri);
    <c:forEach items="${regionList.values}" var="region">
    ri = new Object();
    ri.id = "${region.id}";
    ri.name = "${region.name}";
    regions.push(ri);
    </c:forEach>
    window.regions = regions;

    取数据则 Object region = regions[index];

    var regionId = region.id;

    Note: js中嵌入了<c:foreach>不建议这么用。

    4. bind 与 unbind

    $("#name").bind("click", (function () {
    $("#attrib_UNIQUE_ACTV_PURCHASE").attr('checked', true);
    }));
    $("#description").bind("click", (function () {
    $("#attrib_UNIQUE_ACTV_PURCHASE").attr('checked', false);
    }));
    $("#message").bind("click", (function () {
    $("#name").unbind("click");
    $("#description").unbind("click");
    }));

    增加点击后选中/不选中或解绑后点击效果

    click为eventType,还有其他的

    Mouse EventsKeyboard EventsForm EventsDocument/Window Events
    click keypress submit load
    dblclick keydown change resize
    mouseenter keyup focus scroll
    mouseleave   blur unload

    5.判断元素是否存在:

    How do I test whether an element exists?

    Use the .length property of the jQuery collection returned by your selector:

    1
    2
    3
    4
    5
    if ( $( "#myDiv" ).length ) {
     
    $( "#myDiv" ).show();
     
    }

    Note that it isn't always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no errors) if it does not:

    $( "#myDiv" ).show();

    or:

    It seems some people are landing here, and simply want to know if an element exists (a little bit different to the original question).

    That's as simple as using any of the browser's selecting method, and checking it for a truthy value (generally).

    For example, if my element had an id of "find-me", I could simply use...

    var elementExists = document.getElementById("find-me");
    if (
    elementExists != null){dosomething}

    This is spec'd to either return a reference to the element or null.

  • 相关阅读:
    指定一个M3U8文件,判断它包含的TS文件是不是都存在。指定一个Office生成的Swf文件,判断它包含的Swf文件是不是完整都存在。
    东师理想云平台异步任务处理系统V2.0重构思路
    Mysql查询出所有列名
    IntelliJ IDEA添加jar包
    IDEA下利用Jrebel插件实现JFinal项目main方法【热加载】
    整理OpenResty+Mysql+Tomcat+JFinal+Cannal+HUI
    有没有 linux 命令可以获取我的公网 ip, 类似 ip138.com 上获取的 ip?
    MongoDB学习笔记(5)--document
    MongoDB学习笔记(4)--collection
    MongoDB学习笔记(3)--删除数据库
  • 原文地址:https://www.cnblogs.com/daxiong225/p/8006658.html
Copyright © 2020-2023  润新知