drxdai15012937753 2016-01-19 22:55
浏览 56
已采纳

HTML使用JQuery选择元素验证

I have a form on my website with a select element, which I would like to make required. The form includes selectable parameters for a product and a Buy Now button. I tried to use the required attribute on the select element, but the form can still be submitted without choosing an option. My guess is that the default onclick value on the anchor tag is what causes the conflict.

Is there any way to perform the validation using e.g. JQuery? Or with the standard HTML5 form validation?

You can see the basic structure of the form below. Also, here is a live example: http://www.picassinodesign.com/rendelheto-termekek/lancok/products/display/rasztasal

(I know that the submit button should be inside the form element, and it should be a button or an input, but it is made with the Ecommerce WD component for Joomla.)

<form name="wd_shop_form_order" action="" method="POST">

    <label for="wd_shop_selectable_parameter_Color">Color:</label>

    <select required id="color" name="color" onchange="onSelectableParameterChange(this, event)">
        <option value="">- Select -</option>
        <option value="Color 1">Color 1</option>
        <option value="Color 2">Color 2</option>
    </select>

</form>

<a href="http://www.picassinodesign.com/index.php?option=com_ecommercewd&amp;controller=checkout&amp;task=quick_checkout"
  onclick="wdShop_onBtnBuyNowClick(event, this); return false;">Buy now</a>
  • 写回答

2条回答 默认 最新

  • dtsfnyay300457 2016-01-19 23:09
    关注

    I haven't tried parsley, but it looks good. Another option is jQuery form validate with custom rule, as in this answer.

    Lib: jQuery Form Validate

    Rule: Create Rule

    Another very simple way would disable the submit button until the selector has been clicked twice. That wouldn't be 100%, because they could just double-click, but it would cover a large percentage.

    Another option would be to use event.preventDefault() on submit, check the value yourself and then either submit or popup warning.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题