guyeuro
V2EX  ›  问与答

如何改变 select 元素的值?

  •  
  •   guyeuro · Aug 14, 2017 · 1613 views
    This topic created in 3195 days ago, the information mentioned may be changed or developed.

    前台 html,用的是 springmvc form

    <form:select id="goodSelectId" path="goodId" items="${goodsList}"
          itemLabel="name" itemValue="goodCode">
          <form:option value="">请选择商品</form:option>
    </form:select>
    
    
    <form:checkbox path="flag" id="flag" value="1"
          onchange="change(this, 'goodSelectId')"/> 低消产品
    

    javascript 脚本函数:

        function change(self, targetId) {
            if ($(self).is(':checked')) {
                $("#" + targetId).attr("disabled", true);
                $("#" + targetId).val("");
    
            }
            else {
                $("#" + targetId).attr("disabled", false);
            }
        }
    

    当下面的 checkbox 打钩后,要使得 select 元素的值为空 我用$("#id").val(""); 但是发现不起作用 传到后台“ goodId ”依然有值 怎么回事?

    Supplement 1  ·  Aug 14, 2017
    我把脚本 改成:


    function change(self, targetId) {
    if ($(self).is(':checked')) {
    $("#" + targetId).attr("disabled", true);
    $("#" + targetId).val("333");
    alert(("#" + targetId).val());

    }
    else {
    $("#" + targetId).attr("disabled", false);
    }
    }

    alert 提示信息里是空的,而不是 333,连赋值都赋值不了,怎么回事?
    2 replies    2017-08-14 14:51:55 +08:00
    oh
        1
    oh  
       Aug 14, 2017
    试试 $("#id").removeAttr("value");
    guyeuro
        2
    guyeuro  
    OP
       Aug 14, 2017
    @oh 不行啊
    奇怪,为何$("#id").val("");没用呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5923 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 03:06 · PVG 11:06 · LAX 20:06 · JFK 23:06
    ♥ Do have faith in what you're doing.