0

我有一个占位符值输入框,display:none;由于它受 jQuery 控制,因此设置为页面加载,并显示占位符下拉框何时选择为是。

我的问题是我已经对表单使用了 Codeigniter 验证,如果表单上有错误,页面会重新加载占位符下拉框状态是,但占位符值输入框消失。

我最好的选择是创建一个 callback_ 函数,该函数基本上说明是否选择了显示输入框?

jQuery:

              $("#add_fields_placeholder").change(function()
    {
        if($(this).val() == "yes")
    {
        $('label[for="add_fields_placeholderValue"]').show();
        $("#add_fields_placeholderValue").show();
    }
    else
    {

        $('label[for="add_fields_placeholderValue"]').hide();
        $("#add_fields_placeholderValue").hide();
    }
        });

看法:

<label for="add_fields_placeholder">Placeholder: </label>
<select name="add_fields_placeholder" id="add_fields_placeholder">
    <option value="">Please Select</option>
    <option value="yes" <?php echo set_select('add_fields_placeholder','yes', ( !empty($placeholderType) && $placeholderType == "yes" ? TRUE : FALSE ));?>>Yes</option>
    <option value="no" <?php echo set_select('add_fields_placeholder','no', ( !empty($placeholderType) && $placeholderType == "no" ? TRUE : FALSE ));?>>No</option>
</select>

<label for="add_fields_placeholderValue">Placeholder Text: </label>
<input type="text" name="add_fields_placeholderValue" id="add_fields_placeholderValue" value="<?php echo set_value('add_fields_placeholderValue'); ?>">

控制器验证:

$this->form_validation->set_rules('add_fields_placeholder', 'Placeholder', 'trim|required|xss_clean|callback_dropdown_check');

$this->form_validation->set_rules('add_fields_placeholderValue', 'Placeholder Text','trim|xss_clean');
4

2 回答 2

1

当页面加载时,您需要触发add_fields_placeholder. 只需确保选择了 yes 选项。代码将类似于:

function initChangeEvent() {
  $("#add_fields_placeholder").change(function() {
    if($(this).val() == "yes") {
        $('label[for="add_fields_placeholderValue"]').show();
        $("#add_fields_placeholderValue").show();
    } else {
        $('label[for="add_fields_placeholderValue"]').hide();
        $("#add_fields_placeholderValue").hide();
    }
  });
}

$(function(){
  initChangeEvent();
  $("#add_fields_placeholder").trigger('change');  
})
于 2012-08-15T03:04:56.920 回答
0

您也可以将此代码放在 document.ready 函数中。它会工作

if( $("#add_fields_placeholder").val() == "yes")
{
    $('label[for="add_fields_placeholderValue"]').show();
    $("#add_fields_placeholderValue").show();
}
else
{

    $('label[for="add_fields_placeholderValue"]').hide();
    $("#add_fields_placeholderValue").hide();
}
    });
于 2012-08-15T09:27:52.333 回答