php 用于选择菜单的联系表格 7 水印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13178585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:55:09  来源:igfitidea点击:

Contact Form 7 watermark for select menu

phpwordpresscontact-form-7

提问by Iliya Reyzis

im searching for a way to put a watermark on select fields.

我正在寻找一种在选定字段上放置水印的方法。

that is not working ->

这不起作用->

[select* c_type class:ic watermark "choose type" "a" "b" "c"]

to put a not valid value that fail validation, i had to put include_blank

要放置一个无法验证的无效值,我不得不放置 include_blank

[select* c_type class:ic include_blank "a" "b" "c"]

but the problem is that i have ---as watermark, thats what i want to change..

但问题是我有---水印,这就是我想要改变的..

回答by user5071251

More recent versions of Contact Form 7 allow the use of first_as_label to create placeholder text that does not validate as an entry if users do not make a selection. Simply make your placeholder text be the first label in the list of options.

最新版本的联系表 7 允许使用 first_as_label 创建占位符文本,如果用户未进行选择,则不会作为条目进行验证。只需将占位符文本设为选项列表中的第一个标签即可。

[select* food-choice first_as_label "Preferred food?" "Cake" "Pizza" "Burger" "Salad" "Donut"]

回答by Iliya Reyzis

After hard searching i found this script that is working and replacing the "---" when targeting to that element this one is changing all the "---"s

经过努力搜索,我发现这个脚本正在运行,并在定位到该元素时替换了“---”,这个脚本正在改变所有的“---”

function my_wpcf7_form_elements($html) {
    $text = 'Please select...';
    $html = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $html);
    return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

this code, replacing with targeting

此代码,替换为定位

function my_wpcf7_form_elements($html) {
    function ov3rfly_replace_include_blank($name, $text, &$html) {
        $matches = false;
        preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
        if ($matches) {
            $select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
            $html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
        }
    }
    ov3rfly_replace_include_blank('menu-569', 'Choose language', $html);
    ov3rfly_replace_include_blank('menu-614', 'Choose country', $html);
    return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

hope that will save for some of you a headache (source here)

希望这会让你们中的一些人头疼(来源在这里

回答by Aarzoo Patel

Try this:

尝试这个:

[select* menu-206 first_as_label "Select doctor" "David Mikaberidze" "Sophio Gelashvili" "Maya Dolidze"]

回答by Foley

If you find string replacement unefficient, you could simply use this:

如果你发现字符串替换效率低下,你可以简单地使用这个:

jQuery(function($) {
    $("select option:first").attr('disabled', 'disabled');// Disable the first value/label ---
  });

I also made sure that the first alternative is the "label" i want to use, by adding 'first_as_label' to the shortcode in wcf7, like this:

我还通过将“first_as_label”添加到 wcf7 中的短代码来确保第一个选择是我想要使用的“标签”,如下所示:

[select name first_as_label 'label' 'alt1' 'alt2' 'alt3']

By making the first option disabled, wcf7 won't confirm the form until this an enabled alternative is chosen.

通过禁用第一个选项,wcf7 将不会确认表单,直到选择了启用的替代选项。

回答by Stuart Harrison

You can do it with one line of jQuery.

你可以用一行 jQuery 来完成。

To replace the – - – with the placeholder text I wanted, I first of all gave the fields an ID in the Contact Form 7 options. Following this, I added the following in my themes footer between script tags.

要将 – - – 替换为我想要的占位符文本,我首先在联系表 7 选项中为这些字段指定了 ID。在此之后,我在脚本标签之间的主题页脚中添加了以下内容。

$("#typeofinjury option:first:contains('---')").html('How Were You Injured?');//Replace ---

The code simply looks for the first option inside the dropdown menu which has the ID ‘typeofinjury'. It then replaces it with the text ‘How Were You Injured?'.

该代码只是在下拉菜单中查找 ID 为“typeofinjury”的第一个选项。然后将其替换为文本“您如何受伤?”。

The blog post for this solution with screenshots is Here

带有屏幕截图的此解决方案的博客文章是这里