-1

enter code here我有一个包含许多字段的表格。我需要的是默认情况下禁用底部名称为“Shipping Preference”的下拉菜单,直到填写 2 个或更多项目表单字段。所以填写了 2 个文本字段,下拉菜单将是启用。怎么做?我一点想法都没有。任何帮助将不胜感激。另请注意:我对字段和占位符有限制。这是代码:

 <SCRIPT> $("#Glass_ASTRO_165WNT").on('change', function() {
       this.value = (!Number(this.value)) ? '' : Math.max(100, Math.min(999999, Number(this.value)));
        });

    //With reference to http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
    // 1. Handle placeholders (in non-HTML5 compliant browsers).
    if(!supports_input_placeholder()) {
        $('[placeholder]').focus(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();

        //2. Prevent placeholder values being submitted to form's action script
        $('[placeholder]').eq(0).closest('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }


               });
            });
        }
    });</SCRIPT>

    <form name="amada" method="post" action="review.php"onSubmit="return validate.check(this)">

                  <div class="side1">
                  <article>
                        <h2>Silver Name Badges</h2>
                        <p class="subDetails">Min. Qty: 100 pcs <br>
                          Lead Time: 3 Weeks<br>
                        <img src="web-pics/name_badge.png" width="200" height="200" alt="badge" /><br>

                         Quantity:
                    <input type="text" default=min=100 placeholder="none" name="Silver_Name_Badges" id="Silver_Name_Badges" maxlength="10" style="width:50px" tabindex=1  >

                    </p>
                </article>


 <td valign="top"><select name="Ship_Preference" id="Ship_Preference" tabindex=20 >
       <option value="Partial Shipment">Partial Shipment</option>
       <option value="When All Items Ready">Ship When All Items Ready</option>
       </select></td>
 <td valign="top"><select name="Ship_Preference" id="Ship_Preference" tabindex=20 >
       <option value="Partial Shipment">Partial Shipment</option>
       <option value="When All Items Ready">Ship When All Items Ready</option>
       </select></td>
4

1 回答 1

0

一种可能的解决方案是在输入元素的内容发生变化时触发事件。当事件触发时,您可以检查表单中填充的输入元素的数量(或者换句话说,返回非空value属性)。像这样:

// Listen for 'input' events on all elements (that are not buttons)
// within the form named 'amada'
$("form[name='amada'] input[type!='button']").on("input", function(){
    // Count the number of inputs within this form that are not populated
    if($("form[name='amada"] input[value!=''][type!='button']").length >= 2){
        // If at least 2 populated inputs, enable the "Ship_Preference" select box
        $("select[name='Ship_Preference']").removeAttr("disabled");
    }else{
        // Otherwise, disable it.
        $("select[name='Ship_Preference']").attr("disabled", "disabled);
    }
})

请注意,type!="button"过滤器将从选择中删除按钮。如果这与您的代码无关,则为简单起见将其删除。

另请注意,每当插入一个字符时,“输入”事件(我从这个答案中偷来的)都会触发,而不是“更改”事件,它只会在输入框失去焦点时出现。我认为这是所需的行为,但是您可以尝试查看自己喜欢的行为。

希望有帮助。

于 2013-04-23T16:00:32.117 回答