복붙노트

[JQUERY] jQuery - 파리에 숨겨진 양식 요소를 만듭니다

JQUERY

jQuery - 파리에 숨겨진 양식 요소를 만듭니다

해결법


  1. 1.

    $('<input>').attr('type','hidden').appendTo('form');
    

    두 번째 질문에 답변하려면 :

    $('<input>').attr({
        type: 'hidden',
        id: 'foo',
        name: 'bar'
    }).appendTo('form');
    

  2. 2.

    $('#myformelement').append('<input type="hidden" name="myfieldname" value="myvalue" />');
    

  3. 3.David 's와 동일하지만 attr () 없음

    David 's와 동일하지만 attr () 없음

    $('<input>', {
        type: 'hidden',
        id: 'foo',
        name: 'foo',
        value: 'bar'
    }).appendTo('form');
    

  4. 4.더 많은 속성을 추가하려면 다음과 같이하십시오.

    더 많은 속성을 추가하려면 다음과 같이하십시오.

    $('<input>').attr('type','hidden').attr('name','foo[]').attr('value','bar').appendTo('form');
    

    또는

    $('<input>').attr({
        type: 'hidden',
        id: 'foo',
        name: 'foo[]',
        value: 'bar'
    }).appendTo('form');
    

  5. 5.

    function addHidden(theForm, key, value) {
        // Create a hidden input element, and append it to the form:
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = key; //name-as-seen-at-the-server
        input.value = value;
        theForm.appendChild(input);
    }
    
    // Form reference:
    var theForm = document.forms['detParameterForm'];
    
    // Add data:
    addHidden(theForm, 'key-one', 'value');
    

  6. 6.jsfiddle.

    jsfiddle.

    양식이 같은 경우

    <form action="" method="get" id="hidden-element-test">
          First name: <input type="text" name="fname"><br>
          Last name: <input type="text" name="lname"><br>
          <input type="submit" value="Submit">
    </form> 
        <br><br>   
        <button id="add-input">Add hidden input</button>
        <button id="add-textarea">Add hidden textarea</button>
    

    숨겨진 입력과 TextArea를 추가하여 다음과 같이 형성 할 수 있습니다.

    $(document).ready(function(){
    
        $("#add-input").on('click', function(){
            $('#hidden-element-test').prepend('<input type="hidden" name="ipaddress" value="192.168.1.201" />');
            alert('Hideen Input Added.');
        });
    
        $("#add-textarea").on('click', function(){
            $('#hidden-element-test').prepend('<textarea name="instructions" style="display:none;">this is a test textarea</textarea>');
            alert('Hideen Textarea Added.');
        });
    
    });
    

    JSFIDDLE를 여기에서 확인하십시오

  7. from https://stackoverflow.com/questions/2408043/jquery-create-hidden-form-element-on-the-fly by cc-by-sa and MIT license