duanjia9577 2015-03-12 19:12
浏览 65
已采纳

ajax生成的表单与php插入的值通过javascript函数 - 元素不存在当我通过ID检查它们?

I would like to prepopulate the fields of a form I display in a modal view via jquery after getting the form HTML with an ajax query. I am prepopulating the fields via a document.getElementById call to set the value in a javascript function, with the values in the javascript function generated with php code.

The problem is that the fields I want to populate with values don't seem to exist even after the callback function from the AJAX call has imported the form code. When I check if the elements exist in my function to set the values of those elements, I see a null return, so the elements are not there. What's going on?

I can see that in the html source code, the php has put the correct form values in. However, I do not see the textarea value/innerHTML displayed. Rather the textarea is blank. Is there something tricky about text areas?

Thank you for any suggestions.

Here is the Jquery/javascript:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>


<script>
 $(function() {
     var dialog, form,
         dialog = $( "#dialog-form2" ).dialog({
             autoOpen: false,
             height: 550,
             width: 400,
             modal: true,   
         });

     $( "#create-user2" ).button().on( "click", function() {
         showform();
         dialog.dialog( "open" );
     });
 });
</script>

<div id="dialog-form2" style="background-color: white; border: 4px solid #f1f1f1; padding: 20px;" >

</div>

<div id="dialog-form2" style="background-color: white; border: 4px solid #f1f1f1; padding: 20px;" ></div>

<script>
 function showform()
 {
     var xmlhttp;
     if (window.XMLHttpRequest) {
         xmlhttp=new XMLHttpRequest();
     } else {
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function() {
         if (xmlhttp.readyState==4 && xmlhttp.status==200) {
             document.getElementById("dialog-form2").innerHTML=xmlhttp.responseText;
             updateform();
         }
     }

     xmlhttp.open("GET", "newinstructions.html");
     xmlhttp.send();  
 }


 function updateform()
 {
     document.getElementById('f1').value = '<?php echo $included;?>';
     document.getElementById('f2').value = '<?php echo $id_bus;?>';
     document.getElementById('f3').value = '<?php echo $deals_id;?>';
     document.getElementById('f4').value =  '<?php echo $_SESSION['token'];?>';
     document.getElementById('details').innerHTML = '<?php echo $instructions1;?>';
     document.getElementById('details').value = '<?php echo $instructions1;?>';

 }

 </script>

Here is the form:

<form method = "post" action = "changeinstructions.php">
    <textarea rows = "8" cols = "60" name = "details" id = "details" ></textarea>   
    <input type = "hidden" name = "included" '/>
    <input type = "hidden" name = "id" />
    <input type = "hidden" name = "deals_id"/>
    <input type = "hidden" name = "token" />
    <br>
    <input type="submit" tabindex="-1" >
</form>

And finally I include this in a php file as include_once('jquery.php') and I have verified that all the php variables exist and have values just before this include statement. Here is what the html source code looks like as resulting for the javscript function setting values:

 function updateform()
 {
     document.getElementById('f1').value = '1';
     document.getElementById('f2').value = '59';
     document.getElementById('f3').value = '226';
     document.getElementById('f4').value =  '7a7dd52df381577c7b8e2518aa9b2808';   
     document.getElementById('details').innerHTML = ' dumb instructions';
     document.getElementById('details').value = ' dumb instructions';

 }

So the function updateform() has the right values, and this function should only be called after the form itself has been created. Yet no values appear on the form, and no values appear in the POST array after I submit the form. What gives? Thanks for any advice.

  • 写回答

3条回答 默认 最新

  • duansha6410 2015-03-12 19:16
    关注

    you want to populate the inserted values into a new popup modal or dialog ?? please explain more what you are trying do,

    if iam correct , you dont need to re-include jquery code you can do this in a callback function instead

    ////edit you can use .val() to set or get textarea //edit you are using a get in your ajax , while your form is using POST

    i wounder if you are using $_GET or $_POST in your php , this is confusing //

    the solution as follows :

    // you need to select the textarea not by its id alone, you need to select the top parent first , then second top parent example : if you open you console you will find it something like this (after the popup appear) <div id="popup top div"><div id="another parent"><form><textarea id="details"> , all you need to do is something like var target_textarea =$("#topid #secoundtopid #details") then use the target_textarea.val();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?