使用PHP将复选框转换为下拉列表

时间:2015-12-02 05:23:40

标签: php wordpress select wordpress-plugin

我正在使用WP作业管理器插件,我想将作业类型复选框转换为下拉列表(灰色背景)。

我试图转换它,但我没有运气。如何使用selected()函数执行此操作?感谢

以下是我尝试过的代码段:

<?php if ( ! is_tax( 'job_listing_type' ) && empty( $job_types ) ) : ?>
<ul class="job_types">
    <?php foreach ( get_job_listing_types() as $type ) : ?>
        <select name="filter_job_type[]">
            <option value='Collections' ". selected( $types['collections'], 'collections', false .">Collections</option>
        </select>
   <?php endforeach; ?>

以下是作业类型复选框的片段:

<?php if ( ! is_tax( 'job_listing_type' ) && empty( $job_types ) ) : ?>
<ul class="job_types">
    <?php foreach ( get_job_listing_types() as $type ) : ?>
 <li>
  <label for="job_type_<?php echo $type->slug; ?>" class="<?php echo sanitize_title( $type->name ); ?>">
   <input type="checkbox" name="filter_job_type[]" value="<?php echo $type->slug; ?>" <?php checked( in_array( $type->slug, $selected_job_types ), true ); ?> id="job_type_<?php echo $type->slug; ?>" /> <?php echo $type->name; ?>
 </label>
</li>
<?php endforeach; ?>
</ul>
<input type="hidden" name="filter_job_type[]" value="" />
<?php elseif ( $job_types ) : ?>
<?php foreach ( $job_types as $job_type ) : ?>
    <input type="hidden" name="filter_job_type[]" value="<?php echo sanitize_title( $job_type ); ?>" />
<?php endforeach; ?>

以下是链接:http://bit.ly/1OEzAwz

2 个答案:

答案 0 :(得分:2)

这可能适合你:

<script>
var select=jQuery(document.createElement('select')).insertBefore(jQuery('.load_more_jobs'));
jQuery('.job_listing a').each(function(){
    jQuery(document.createElement('option')).appendTo(select).val(this.href).html(jQuery(this).html());
});
jQuery('.entry-content select').on('change',function(){
    if(!this.value)return;
    console.log(this.options[this.selectedIndex].innerHTML+": "+this.value);
    window.location.href=this.value;
});
jQuery('.job_listing').hide();
</script>

答案 1 :(得分:1)

您可以在头标记处尝试这个

<script>
  (function($) {
    "use strict"

    $(function() {
      var $job_types_select = $('<select class="job_types_select"></select>');
      var $job_types_ul = $('form.job_filters ul.job_types');
      var $job_type_hidden = $('<input type="hidden" name="filter_job_type[]"/>');
      $job_types_ul.find('li').each(function() {
        var $li = $(this);
        var label_text = $li.find('label').text();
        var value = $li.find('input:checkbox').val();
        var $option = $('<option></option>');
        $option.text(label_text);
        $option.attr({value: value});
        $job_types_select.append($option);
      });
      $job_types_select.change(function() {
        var value = $(this).val();
        $('input:hidden[name="filter_job_type[]"]').val(value);
        var target   = $( this ).closest( 'div.job_listings' );
        target.triggerHandler( 'update_results', [ 1, false ] );
      });
      $job_types_ul.after($job_type_hidden);
      $job_types_ul.replaceWith($job_types_select);
    });
  })(jQuery);
</script>