Create a hidden field in JavaScript

后端 未结 4 894
说谎
说谎 2020-11-30 05:47

How do you create a hidden field in JavaScript into a particular form ?

    
    
      

        
相关标签:
4条回答
  • 2020-11-30 05:54

    You can use this method to create hidden text field with/without form. If you need form just pass form with object status = true.

    You can also add multiple hidden fields. Use this way:

    CustomizePPT.setHiddenFields( 
        { 
            "hidden" : 
            {
                'fieldinFORM' : 'thisdata201' , 
                'fieldinFORM2' : 'this3' //multiple hidden fields
                .
                .
                .
                .
                .
                'nNoOfFields' : 'nthData'
            },
        "form" : 
        {
            "status" : "true",
            "formID" : "form3"
        } 
    } );
    

    var CustomizePPT = new Object();
    CustomizePPT.setHiddenFields = function(){ 
        var request = [];
    	var container = '';
    	console.log(arguments);
    	request = arguments[0].hidden;
        console.log(arguments[0].hasOwnProperty('form'));
    	if(arguments[0].hasOwnProperty('form') == true)
    	{
    		if(arguments[0].form.status == 'true'){
    			var parent = document.getElementById("container");
    			container = document.createElement('form');
    			parent.appendChild(container);
    			Object.assign(container, {'id':arguments[0].form.formID});
    		}
    	}
    	else{
    		 container = document.getElementById("container");
    	}
    	
    	//var container = document.getElementById("container");
    	Object.keys(request).forEach(function(elem)
    	{
    		if($('#'+elem).length <= 0){
    			console.log("Hidden Field created");
    			var input = document.createElement('input');
    			Object.assign(input, {"type" : "text", "id" : elem, "value" : request[elem]});
    			container.appendChild(input);
    		}else{
    			console.log("Hidden Field Exists and value is below" );
    			$('#'+elem).val(request[elem]);
    		}
    	});
    };
    
    CustomizePPT.setHiddenFields( { "hidden" : {'fieldinFORM' : 'thisdata201' , 'fieldinFORM2' : 'this3'}, "form" : {"status" : "true","formID" : "form3"} } );
    CustomizePPT.setHiddenFields( { "hidden" : {'withoutFORM' : 'thisdata201','withoutFORM2' : 'this2'}});
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id='container'>
    
    </div>

    0 讨论(0)
  • 2020-11-30 06:01
    var input = document.createElement("input");
    
    input.setAttribute("type", "hidden");
    
    input.setAttribute("name", "name_you_want");
    
    input.setAttribute("value", "value_you_want");
    
    //append to form element that you want .
    document.getElementById("chells").appendChild(input);
    
    0 讨论(0)
  • 2020-11-30 06:01

    I've found this to work:

    var element1 = document.createElement("input");
    element1.type = "hidden";
    element1.value = "10";
    element1.name = "a";
    document.getElementById("chells").appendChild(element1);
    
    0 讨论(0)
  • 2020-11-30 06:12

    You can use jquery for create element on the fly

    $('#form').append('<input type="hidden" name="fieldname" value="fieldvalue" />');
    

    or other way

    $('<input>').attr({
        type: 'hidden',
        id: 'fieldId',
        name: 'fieldname'
    }).appendTo('form')
    
    0 讨论(0)
提交回复
热议问题