2016-05-12 95 views
1

我將一些值從html傳遞給javascript方法,並且我想獲取html元素的內容。獲取jQuery的輸出到html輸出

$('#qtyorder span').each(function(){ 
     var id =($(this).prop('id')); 
     var thenum = id.replace(/^\D+/g, ''); 
     var qty=$("#qty_ordered_"+thenum).html(); 

      if($("#qty_rec"+thenum).val() == ''){     
       $("#qty_rec"+thenum).val(qty); 
       jQuery('#ordDate'+thenum).val($.datepicker.formatDate('d/mm/yy', new Date())); 
       $("#ordDate"+thenum).trigger('click'); 

       } 

我的HTML形式是這樣的,在這裏我想用ID

<form 
    class="overlay_form width_control_473 receive_autofil_popup padding_control_bottom_20" 
     <input type="hidden" id="qty_rec" value="" /> <input type="hidden" 
      id="qty_rec" value="" /> <input type="hidden" id="dateRcvd" 
      value="" /> <input type="hidden" id="p_on" value="" /> <label 
      class=" label_break_b verd12dgray">Item Code</label> 
     <div class="clear_fix"></div> 
     <input 
      class="input_md_rd radious_all innershadow padding_control width_control_168" 
      name="" type="text" id="code"> 
    </div> 

    <div class="pur_field_container float_left"> 
     <label class=" label_break_b verd12dgray">Quantity Ordered</label> 
     <div class="clear_fix"></div> 
     <input 
      class="input_md radious_all innershadow padding_control width_control_168" 
      name="" type="text" id="qty_rec"> 
    </div> 
    <div class="pur_field_container "> 
     <label class=" label_break_b verd12dgray">Quantity Received</label> 
     <div class="clear_fix"></div> 
     <input 
      class="input_md radious_all innershadow padding_control width_control_168" 
      name="" type="text" id="qty_rec" 
      onkeyup="cancelOrderAutofilNotification()"> 
      <input 
      class="input_md radious_all innershadow padding_control width_control_168" 
      name="" type="text" id="qty_rec" 
      onkeyup="confirmOrderAutofilNotification()"> 
    </div> 
    <div 
     class="field_container_b width_control_317 margin_control_top float_left"> 
     <input 
      type="button" value="Cancel" 
      class="close button_gray_md1 radious_all verd12dgray float_left margin_control_top_10 " 
      name=""> 
    <input type="button" value="Confirm" 
      class="confirm button_gray_md1 radious_all verd12dgray float_left margin_control_top_10 " 
      name="" onclick="confirmOrderAutofilNotification();"> 

    </div> 
</form> 

如何JavaScript的值傳遞給這種形式來傳遞價值?

+1

嘗試創建元素和'$(父).append()'或'$(元素)的.html(HTML)'? – Shreedhar

回答

0

說你想,因爲你已經在使用jQuery的從你的JavaScript來更新與輸出一個div

<div id = "content"></div> 

與普通的JavaScript可以在div填補像這樣

document.getElementById('content').innerHTML = "Your HTML here"; 

,你可以使用

$('#content').html("Your HTML here"); 
+0

好吧,在我的情況下,我有一個$(「#qty_rec」+ thenum).val(qty);可以說我想得到數量,你怎麼能把它傳遞給這個$('#content')。html(「你的HTML在這裏」);?我有點困惑 – jdoe

+0

@jdoe這樣做$(「#qty_rec」)。html(qty); –

+0

我試過,我在console.log中得到一個空值 – jdoe

1

在JavaScript中,我們有2個方法來設置元素.val()和.html()的值。您可以使用.val()輸入元素,如文本輸入或選擇輸入。 .html()用於span,div,p,labels等元素,在這種情況下,您應該使用.html()。

+0

你的意思是這樣的? var it = $(「#qty_rec」+ thenum).val(qty); \t \t \t console.log(it); \t \t \t \t \t \t $( '#qtyrcd')的html =它。 – jdoe

+0

如果「it」有數據,則使用$('#qtyrcd')。htm(it); –

+0

這就是我在console.log中輸入#qty_rec0.input_md.radious_all.index.width =「2」屬性值=「」 – jdoe

0

下面是使用jQuery來改變HTML和輸入值的活生生的例子..

使用$('element').val()改變輸入值和$('element').html() HTML內容的變化......

$(document).ready(function(){ 
 
    $('#change').click(function(){ 
 
    $('#myInput').val('XYZ'); /// element.val() for inputs 
 
    $('#myDiv').html('Div content changed'); //// element.html() for changing html content 
 
    }) 
 
    
 
    $('#reset').click(function(){ 
 
    $('#myInput').val('ABC'); 
 
    $('#myDiv').html('Here is div content'); 
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id=myDiv>Here is div content</div> 
 

 
<br> 
 

 
First Name: <input type="text" id="myInput" name="firstname" value="ABC" /> 
 

 
<br><br> 
 

 
<button id="change">Change Content</button> 
 
<button id="reset">Reset Content</button>

0

在Jquery中使用HTML值:

//tags who uses value attributes 
var jqVar = $('#HTML-tag-id').val(); 
//other HTML tags 
var jqVar = $("#HTML-tag-id").html(); 
在HTML

設置的jQuery值:

var jqVar = $('#HTML-tag-id').val(); 

//putting jqVar value to some other textbox 
$('#HTML-tag-id2').val(jqVar); 
//using it in other HTML tag 
$('#HTML-tag-id2').html(jqVar);