简体   繁体   English

如果选项文本包含“选择”,则选择框验证

[英]Select Box Validation if Option Text Contains Select

If I have a select box, with the text Select (Insert Type of Addon) 如果我有一个选择框,则显示文本选择(插入插件的类型)

For example, with the text "Select Case and Clip" as the default option as well as 5 other select boxes, I want to validate this form only if no select box contains the word "Select" in it. 例如,使用文本“选择案例和剪辑”作为默认选项以及5个其他选择框,仅当没有选择框中包含“选择”字样时,我才想验证此表单。

This is using Virtuemart, and there is no way to make sure the options are selected properly. 这使用的是Virtuemart,无法确保正确选择了选项。

My idea, would be to use the method above and find the value Select in an options text, on submission 我的想法是,使用上述方法并在提交时在选项文本中找到值“选择”。

 <select name="customPrice[0][27]" id="customPrice027">
     <option value="265">Select Case &amp; Clip</option>
     <option value="266">$25.00 - Black - Large - 1</option>
     <option value="267">$0.00 - Black - None - 2</option>
     <option value="268">$10.00 - Beige - Small - 3</option>
     <option value="269">$20.00 - Beige - Large - 4</option>
     <option value="270">$0.00 - Beige - None - 5</option>
     <option value="271">$10.00 - Blue - Small - 6</option>
     <option value="272">$20.00 - Blue - Large - 7</option>
     <option value="273">$0.00 - Blue - None - 8</option>
     <option value="274">$10.00 - Gray - Small - 9</option>
     <option value="275">$20.00 - Gray - Large - A</option>
     <option value="276">$0.00 - Gray - None - B</option>
     <option value="277">$20.00 - Red - Large - R</option>
     <option value="278">$20.00 - Olive Drab - Large - D</option>
 </select>

My issue is that this is dynamically generated, so I cannot validate and have the first option contain a 0 value. 我的问题是,这是动态生成的,因此我无法验证,并且第一个选项包含0值。

I think using regEx may be the way to go to find the word "Select" in my post. 我认为使用regEx可能是在我的帖子中找到“选择”一词的方法。

Apologize if this is stupid (I am a beginner with jQuery). 抱歉,这是愚蠢的(我是jQuery的初学者)。

Here is my code below, trying to just add a console message saying there is a Select box, with text containing "Select" 这是我的以下代码,尝试仅添加一条控制台消息,说有一个“选择”框,其中包含“选择”文本

    $(document).ready(function () {

       if ($("select").indexOf("Select") >= 1) {
          console.log('Select is Selected');
       }       

    });

Any pointers here? 这里有指针吗? I'd also like this to also validate upon submit button press, before allowing it to do so. 我还希望在允许提交按钮按下之前进行验证。

Update: 更新:

My code is dependent and has to stop the form submission, on selection of the button. 我的代码是依赖的,必须在选择按钮后停止提交表单。 I know the ID, Type, Name, Title and Value of the submission button (pre-defined) 我知道提交按钮的ID,类型,名称,标题和值(预定义)

This is one out of 6 boxes, all same condition fits and triggers this message. 这是6盒中的1盒,所有相同条件都适合并触发此消息。

Here is my code, it isn't stopping on submit. 这是我的代码,不会停止提交。 I can send a link in private message if need be for the site example. 如果需要网站示例,我可以发送私人消息中的链接。

     $(document).ready(function () {

       $( ".addtocart-button" ).click(function() {

        if($("option:contains('Select')").length){
           event.preventDefault();
           console.log("Select found");
        }


    });

}); });

Use the contains selector 使用包含选择器

$("form").submit(function(e){
    if($("#customPrice027 option:selected:contains('Select')").length){
       e.preventDefault();
       alert("found");
    }
});

JS Fiddle: http://jsfiddle.net/QcEbL/1/ JS小提琴: http //jsfiddle.net/QcEbL/1/

Modified to work with your provided code 修改以使用您提供的代码

$( ".addtocart-button" ).click(function(e) {

    if($("#customPrice027 option:selected:contains('Select')").length){
       e.preventDefault();
       console.log("Select found");
    }
});

JS Fiddle: http://jsfiddle.net/QcEbL/2/ JS小提琴: http //jsfiddle.net/QcEbL/2/

Why use the option text to find a select word in the select box if you can do that using the value 如果可以使用value ,为什么要使用option text在选择框中查找选择词

$('#sub').on('click', function() { 
   var selectval = $('#customPrice027').val();
   if(selectval == '265') { 
      alert("Gotcha!");
   }
});

See fiddle 小提琴

Hope it helps! 希望能帮助到你!

Sounds like you would like the user to select something other than the default value, if so check the text of the selected option 听起来您希望用户选择默认值以外的其他选项,如果是,请检查所选选项的文本

var valid = true;

$('select').each(function() {
    if ($('option:selected', this).text().indexOf('Select') != -1) valid = false;
    return valid;
});

FIDDLE 小提琴

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

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