zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    吃推荐3个最近去了的好地方
    30岁生日
    今天开始试用Briglow Hair Cream
    WPF中如何在文本外面加虚线外框
    对账算法改进
    如何退出正在Sleep的线程
    关于framework4.5的相关介绍
    恐怖的报警邮件
    对帐引擎2个月后的监控数据
    wcf rest服务启用gzip压缩
  • 原文地址:https://www.cnblogs.com/daxiong225/p/8006658.html
Copyright © 2011-2022 走看看