添加一个选项以在jquery中动态选择

时间:2016-06-06 13:21:44

标签: jquery sql select options

我有一个在jquery中动态创建的表单,当我点击按钮时出现,我在这个表单中有一个选项,显示来自sql查询的选项。

这是我的代码:



<script>
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    
    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
			 $(wrapper).append(
            $('<form />', { action: 'sharer.php', method: 'POST', class: 'myform'}).append(
                $('<div />', { class: 'appm' }),
                $('<span />', {  text: 'Ref de la Vente' }),
                $('<input />', { id: 'rname', name: 'routename', placeholder: 'Name', type: 'text' }),
				$('<br />'),
				$('<span />', {  text: 'Fournisseur' }),
                $('<input />', { class: 'address', id: 'rdescription', name: 'routedescription', placeholder: 'description', type: 'text' }),
				$('<br />'),
				$('<span />', {  text: 'Ref Produit' }),
                $('<input />', { id: 'tags', name: 'routetags', placeholder: 'tags', type: 'text' }),
				$('<br />'),
				$('<span />', {  text: 'Quantité' }),
				$('<input />', { class: 'address', id: 'rdescription', name: 'routedescription', placeholder: 'description', type: 'text' }),
				$('<br />'),
				$('<span />', {  text: 'Prix d\'Achat' }),
				$('<input />', { class: 'address', id: 'rdescription', name: 'routedescription', placeholder: 'description', type: 'text' }),
				$('<br />'),
				$('<span />', {  text: 'Prix de Vente' }),
				$('<input />', { class: 'address', id: 'rdescription', name: 'routedescription', placeholder: 'description', type: 'text' }),
				$('<br />'),
				$('<span />', {  text: 'Montant Total' }),
				$('<input />', { class: 'address', id: 'rdescription', name: 'routedescription', placeholder: 'description', type: 'text' }),
                $('<br />'),
 
					<?php
					$sql="Select Supplier_Name from suppliers";
					$sups = db_query($database_name, $sql);
					$numItems = count($sups);
					$i = 0;
					?>
						$( '<select/>', {'id': 'deviceType'  ,'type': 'select',	'name': 'deviceType','value': 'One'} ).append(
						<?php
						foreach($sups as $sup=>$s){
							 if(++$i === $numItems) {
								?>
								 $("<option></option>").attr("value", '<?php echo $s['Stock_Name']; ?>').text('<?php echo $s['Stock_Name']; ?>')
								<?php
							 }
								?>
								
						$("<option></option>").attr("value", '<?php echo $s['Stock_Name']; ?>').text('<?php echo $s['Stock_Name']; ?>'),
							<?php
								}    		
							?>
						),
						
				 $('<input />', { id: 'savebutton', type: 'submit', value: 'Save' }),
				'<a href="#" class="remove_field">Remove</a></div>'
				
            )
        )
		
        }
    });
    
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('form').remove(); x--;
    })
});
</script>
&#13;
<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div></div>
</div>
&#13;
&#13;
&#13; 表格没有出现。 我需要纠正什么?

0 个答案:

没有答案