简体   繁体   English

多 <option>值js警报和onclick块

[英]multiple <option> value js alert and onclick block

I am setting up a simpleCart(js) with some selectable options. 我正在设置带有一些可选选项的simpleCart(js)。 I need to show an alert if not all drop-downs meet some value, and also to block the "Add to cart" from adding items to the cart (if not all drop-downs have met some value) This is as far as I manage, any help is highly appreciated . 如果不是所有下拉菜单都满足某个值,我就需要显示警报,还要阻止“添加到购物车”将项目添加到购物车(如果不是所有下拉菜单都满足某个值),就我而言管理,我们将不胜感激。

The HTML HTML

        <p>
        <div class="simpleCart_shelfItem">
        <h2 class="item_name" style="display:none">TEST</h2>
        <select id"sizeSelect" class="item_size">
        <option value="nul">Please choose size</option>
        <option value="small">Small</option>
        <option value="medium">Medium</option>
        <option value="large">Large</option>
        <option value="xlbrain">Super brain</option>
        </select>

        <select id="shippingSelect" onchange="simpleCart.update();">
        <option value="nul">Please choose shipping</option>
        <option value="ups">UPS Standard 25€</option>
        <option value="mail">Standard Mail 10€</option>
        </select>


        <select id"destiantionSelect" class="item_price">
        <option value="nul">Please choose destination</option>
        <option value="290.00">EU</option>
        <option value="220.00">World</option>
        </select></p>

        <p><a class="item_add" onclick="selected()" href="javascript:;">Add to Cart </a></p>
        </div>


The shipping cost added to the grand total 运费合计为总计

        <script type="text/javascript">
        simpleCart.shipping = function(){
        if( $("#shippingSelect").val() == "nul" ){return 0;}    
        if( $("#shippingSelect").val() == "ups" ){return 25;}
        if( $("#shippingSelect").val() == "mail" ){return 10;}
        };
        </script>


The alert 警报

        <script type="text/javascript">
        function selected() {
        var ddl = document.getElementById("shippingSelect","sizeSelect","destinationSelect");
        var selectedValue = ddl.options[ddl.selectedIndex].value;
        if (selectedValue =="nul")
        { alert("Please choose one from all options")}
        }

        </script>

I changed the a button to a actual button and used jquery and binded the click event to check the option boxes, and return if they matched nul, also you have some missing = on your id's. 我将a按钮更改为实际按钮,并使用了jquery并绑定了click事件以检查选项框,如果它们与nul相匹配,则返回它们,并且ID上还缺少=。

$('.item_add').click(function () {
if($('#sizeSelect').val().match(/nul/)) return alert("Please choose a size");
if($('#shippingSelect').val().match(/nul/)) return alert("Please choose shipping type");
if($('#destinationSelect').val().match(/nul/)) return alert("Please enter a destination");
});

http://jsfiddle.net/y2Cf9/ http://jsfiddle.net/y2Cf9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM