我是Contact Form 7的忠实粉丝,我总是想到我需要对表单进行一些扩展自定义。这一次,我很沮丧地试图在选择元素<option>
标签中添加不同的类而无济于事。
我要做的是对从Here到my own CF7 form的下拉列表实施一种很酷的风格和效果 - 因为屏幕截图显示效果很好但是,图标没有显示,因为这样它们可以显示在select元素中的<option>
标记需要有自己的类。
例如:
首先,我需要创建一个id =“cd-dropdown”和class =“cd-select”的select元素,直到这里,这可以通过CF7短代码生成器轻松实现。
[select* select-profissao id:cd-dropdown class:cd-select "Professional" "Nurse" "Lawyer"]
联系表单7上述短代码会生成html select元素,如下所示:
<select id="cd-dropdown" class="cd-select">
<option value="" selected>Professional</option>
<option value="" >Nurse</option>
<option value="" >Lawyer</option>
</select>
但我希望能够在<option>
标记中添加一个类。甚至可以通过使用CF7短代码生成器实现这一目标吗?是否有任何变通方法可以通过使用javascript / jQuery甚至PHP来实现这一目标?
<select id="cd-dropdown" class="cd-select">
<option value="" selected>Professional</option>
<option value="" class="icon-nurse">Nurse</option>
<option value="" class="icon-lawyer">Lawyer</option>
</select>
我真的很感激有关这个问题的任何指导。 提前谢谢。
答案 0 :(得分:3)
只需添加此jquery: http://jsfiddle.net/rvpatel/7wa3V/
$( "#cd-dropdown option" ).addClass(function(index) {
return "icon-" + $(this).text().toLowerCase().split(' ').join('-');
});
答案 1 :(得分:1)
我认为使用jQuery向选项客户端添加类会更容易(假设您已经在SimpleDropDownEffects插件中使用了jQuery)
示例选择按联系表单呈现:
<select id="cd-dropdown" class="cd-select">
<option value="-1" selected>Choose a weather condition</option>
<option value="1">Sun</option>
<option value="2">Clouds</option>
<option value="3">Snow</option>
<option value="4">Rain</option>
<option value="5">Windy</option>
</select>
在页面上添加以下javascript:
jQuery(document).ready(function() {
myClassNames = ["", "icon-sun", "icon-cloudy", "icon-weather", "icon-rainy", "icon-windy"];
jQuery.each(jQuery("#cd-dropdown option"), function(index, value) {
jQuery(value).addClass(myClassNames[index]);
});
//do SimpleDropDownEffects plugin here after classes are added.
});
优点:没有侵入插件文件,没有更新插件问题 缺点:类名已在js中编码
答案 2 :(得分:0)
修改你的插件目录modules/select.php
wpcf7_select_shortcode_handler函数:
function wpcf7_select_shortcode_handler( $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( empty( $tag->name ) )
return '';
$validation_error = wpcf7_get_validation_error( $tag->name );
$class = wpcf7_form_controls_class( $tag->type );
if ( $validation_error )
$class .= ' wpcf7-not-valid';
$atts = array();
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_option( 'id', 'id', true );
$atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
if ( $tag->is_required() )
$atts['aria-required'] = 'true';
$atts['aria-invalid'] = $validation_error ? 'true' : 'false';
$defaults = array();
if ( $matches = $tag->get_first_match_option( '/^default:([0-9_]+)$/' ) )
$defaults = explode( '_', $matches[1] );
$multiple = $tag->has_option( 'multiple' );
$include_blank = $tag->has_option( 'include_blank' );
$first_as_label = $tag->has_option( 'first_as_label' );
$name = $tag->name;
$values = $tag->values;
$labels = $tag->labels;
$empty_select = empty( $values );
if ( $empty_select || $include_blank ) {
array_unshift( $labels, '---' );
array_unshift( $values, '' );
} elseif ( $first_as_label ) {
$values[0] = '';
}
$html = '';
$posted = wpcf7_is_posted();
foreach ( $values as $key => $value ) {
$selected = false;
// changed here
if (! ( $attributes = json_decode($value, true) ) ) {
$attributes = array(
'value' => $value
);
} else {
$value = (isset($attributes['value'])) ? $attributes['value'] : null;
}
if ( $posted && ! empty( $_POST[$name] ) ) {
if ( $multiple && in_array( esc_sql( $value ), (array) $_POST[$name] ) )
$selected = true;
if ( ! $multiple && $_POST[$name] == esc_sql( $value ) )
$selected = true;
} else {
if ( ! $empty_select && in_array( $key + 1, (array) $defaults ) )
$selected = true;
}
// changed here
$item_atts = array('selected' => $selected ? 'selected' : '' );
$item_atts = array_merge($attributes, $item_atts);
$item_atts = wpcf7_format_atts( $item_atts );
$label = isset( $labels[$key] ) ? $labels[$key] : $value;
$html .= sprintf( '<option %1$s>%2$s</option>',
$item_atts, esc_html( $label ) );
}
if ( $multiple )
$atts['multiple'] = 'multiple';
$atts['name'] = $tag->name . ( $multiple ? '[]' : '' );
$atts = wpcf7_format_atts( $atts );
$html = sprintf(
'<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',
$tag->name, $atts, $html, $validation_error );
return $html;
}
现在您可以像以前一样调用插件,或者发送json的值(所有键呈现为属性),即:
[select* select-profissao id:cd-dropdown class:cd-select '{"value":"Professional","class":"mytestclass"}' '{"value":"Nurse","more-attr":"Nurse Attribute"}']
不幸的是我无法测试这个(没有安装wordpress)。