简体   繁体   中英

It is displaying '[Object] [object]' within textarea

I Have an application here where I want to add a "Question" into a textarea but instead of displaying the "Question" within a textarea, it is displaying "[Object] [object]" within the textarea, how can I get it to display the question selected into the testarea?

To use the application follow these steps:

  1. When you open app click on Green Plus button, this will open up a modal window.
  2. You will see a search bar in the modal window, type in "AAA" in search bar and click "Search"
  3. You will see results of the search appear. Click on an "Add" button next to one of the rows and what I want is the content within the "Question" field row to be displayed in the textarea. At the moment it is displaying "[Object] [object]".

Below is the code:

<head>
<script type="text/javascript">

var plusbutton_clicked;

function plusbutton() { 
    // Display an external page using an iframe 
    var src = "previousquestions.php"; 
    $.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
    return false;
} 


function closewindow() {     

    $.modal.close(); 
    return false;
} 

$('.plusimage').live('click', function() {
    plusbutton($(this));
});


function plusbutton(plus_id) {
    // Set global info
    plusbutton_clicked = plus_id;
    // Display an external page using an iframe
    var src = "previousquestions.php";
    $.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
    return false;
}


function addwindow(questionText) { 

    if(window.console) console.log();
        var txt = $(this).val(questionText);

    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') { 
        $('#mainTextarea').val(txt);
        }

    $.modal.close(); 
    return false;
} 


</script>

</head>

<body>

<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">

<div id="detailsBlock">
<table id="question">
<tr>
    <td rowspan="3">Question:</td> 
    <td rowspan="3">
        <textarea class="questionTextArea" id="mainTextarea" rows="5" cols="40" name="questionText"></textarea>
    </td>
</tr>
</table>

<table id="plus" align="center">
<tr>
<th>
<a onclick="return plusbutton();">
<img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" id="mainPlusbutton" name="plusbuttonrow"/>
</a>
<span id="plussignmsg">(Click Plus Sign to look <br/> up Previous Questions)</span>
</th>
</tr>
</table>

</div>
<hr/>

</form>

</body> 

Below is the code which outputs the "Question" field in the search result:

<?php 

$output = ""; 

        while ($questionrow = mysql_fetch_assoc($questionresult)) { 
$output .= " 
<table> 
      <tr> 
      <td class='addtd'><button type='button' class='add' onclick='parent.addwindow();'>Add</button></td> 
      </tr>"; 
        } 
        $output .= "        </table>"; 

        echo $output; 

        ?>  

Your addwindow function is trying to set the value of the mainTextarea to a jQuery object, not a string. Comments inline.

function addwindow(questionText) {

    // this line tries to set the `value` property of window to questionText, which makes no sense
    var txt = $(this).val(questionText);

    // also, that line returns the jQuery object (because of chaining), so `txt` is now a jQuery object holding the window object        

    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
        $('#mainTextarea').val(txt);  // txt is the jQuery object, stringified to [object Object]
    }
    ....
}

The line var txt = $(this).val(questionText); makes no sense whatsoever, because it sets the value of this (which is currently the window object) and sets txt to a jQuery object. Perhaps you meant txt = questionText; ? I am having tremendous difficuly following your code due to all the global variables and irregular indentation.

When you call parent.addWindow() you're not passing an argument and the function expects one.

var txt = $(this).val(questionText); sets txt to $(this) .

That is why it is printing out [object object] .

Edit: fixed bad information.

Edit: added below based on comments

function addwindow(questionText) { 
    if(window.console) console.log();
    var txt = $(this).val(questionText);
    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') { 
        $('#mainTextarea').val(txt);
    }
    $.modal.close(); 
    return false;
}

Should be:

function addwindow(questionText) { 
    if(window.console) console.log();
    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') { 
        $('#mainTextarea').val(questionText);
    }
    $.modal.close(); 
    return false;
}

<?php 
$output = ""; 
while ($questionrow = mysql_fetch_assoc($questionresult)) { 
    $output .= " 
    <table> 
        <tr> 
            <td class='addtd'><button type='button' class='add' onclick='parent.addwindow();'>Add</button></td> 
        </tr>"; 
} 
$output .= "        </table>"; 
echo $output; 
?>

Should be:

<?php
$output = "";
while ($questionrow = mysql_fetch_assoc($questionresult)) {
    $output .= '
    <table>
        <tr>
            <td class="addtd"><button type="button" class="add" onclick="parent.addwindow(\''.$questionrow['text'].'\');">Add</button></td>
        </tr>';
}
$output .= "        </table>";
echo $output;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM