转换textarea中的输入字段

时间:2015-11-29 22:31:26

标签: javascript jquery

我有一张表格。在此表单中,我有一个public function captchahelper() { $helper=array('html','url','string','captcha','form','security'); $this->load->helper($helper); $this->load->library('form_validation'); $this->load->model('model_captcha'); $random_word = random_string('alpha',5); $vals = array( 'word' => $random_word, 'img_path' => './captcha/', 'img_url' => base_url().'captcha/', 'font_path' => './assets/fonts/arial.ttf', 'img_width' => '150', 'img_height' => '35', 'expiration' => '7200', 'font_size' => '16', 'colors' => array( 'background' => array(255, 255, 255), 'border' => array(255, 0, 0), 'text' => array(0, 0, 0), 'grid' => array(255, 40, 40) ) ); $img = create_captcha($vals); $captcha_data = array( 'captcha_time' => $img['time'], 'ip_address' => $this->input->ip_address() , 'word' => $img['word'], ); $data['captcha']= $img; $this->form_validation->set_rules('captcha','Captcha','required|trim'); if ($this->form_validation->run() ==FALSE){ //if any of the above validation fails $data['title']= "DIRECTORY HELPER"; //in view $title $data['page_header'] = '<strong>Directory helper in codeigniter</strong>'; $this->load->view('captchaHelper', $data); }else { $input = xss_clean($_POST['captcha']); $word = $img['word']; echo "word is of type: ".gettype($word); echo br(); echo "POST is of type: ".gettype($input); echo br(2); echo $comp = strcmp($input, $word); echo br(); echo ($input === $word)? 'Match!' : 'NOT A MATCH!'; } 字段。我想更改文本区域中的一些input字段。我从外部源接收此表单,因此我无法更改html。如何在js / jquery中创建它?

这是一个例子:

input

已使用此代码解决:

<label for="MMERGE12" class="MMERGE12-label"><span class="MMERGE12-label">categorie trattate</span>
                                                                        <input id="yikes-easy-mc-form-1-MMERGE12" name="MMERGE12" placeholder="" class="yikes-easy-mc-text" type="text" value="">

                                    <!-- description -->
                                    <p class="form-field-description"><small></small></p>                                   
                                                                        </label>

2 个答案:

答案 0 :(得分:0)

    //Create a new textarea
    var textarea = jQuery("<textarea></textarea>");

    //Select the input 
    var input = jQuery("#yikes-easy-mc-form-1-MMERGE12");

    //Asign the value, id, name from the input to the textarea
    textarea.val(input.val());
    textarea.attr('id', input.attr('id'));
    textarea.attr('name', input.attr('name'));
    //You can copy any other attribute you like here

    //Finally do the replacement
    input.reaplceWith(textarea);

如果你在加载时加载它,你可能需要将它包装在准备好的包装器jQuery(document).ready(function() { /* code here */ })中,或者如果你用ajax加载它,只需在数据附加到DOM后执行此代码

答案 1 :(得分:0)

试试这个

$(document).ready(function () {
        var input = $("#yikes-easy-mc-form-1-MMERGE12");
        var textArea = $("<textarea></textarea>").attr({
            id: input.attr("id"),
            name: input.attr("name"),                    
            value: input.val()
        });
        // class is set separately because "class" is a reserved word ...
        textArea.attr("class", input.attr("class"));
        //insert textarea right after original input, and remove input
        input.after(textArea).remove();
});