如何将textarea值转换为可变的外部函数

时间:2018-11-30 14:36:14

标签: javascript jquery

如何使变量“ apry”等于“ textarea”中的写入数据,

那么我可以将其值输入URL吗?

HTML:

<textarea id="post" type="text"></textarea>
<a onclick="location.href = 'http://localhost/arany/?i=' + apry + '';">Reload</a>

JS:

$(document).ready(function() { 
  $('#post').keyup(function() { 
    var apry = document.getElementById('post').value; 
  }); 
})

5 个答案:

答案 0 :(得分:1)

在这种情况下,不需要全局变量。您只需在重新加载按钮上监听click事件:

<textarea 
id="post" type="text"></textarea>
<button id="reload">
Reload

$(document).ready( function() { 
    $('#reload').click(function() {
        location.href = 'http://localhost/arany/?i=' + 
        $('#post').val(); 
    });
});

http://jsfiddle.net/4j0fohr5/1/

答案 1 :(得分:1)

您实际上是在设置apry的值,但是问题是您然后对它不做任何事情,包括不更新DOM元素。您将需要以下内容:

subsetTask
subset

答案 2 :(得分:0)

执行您需要的操作将更有意义。无需创建和更新具有键入值的am4core.useTheme(am4themes_animated); // Create chart instance var chart = am4core.create("chartdiv", am4charts.XYChart); // Add data chart.data = [{ "category": "Research", "value": 450 }, { "category": "Marketing", "value": 1200 }, { "category": "Distribution", "value": 1850 }]; // Create axes var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis()); categoryAxis.dataFields.category = "category"; categoryAxis.renderer.grid.template.location = 0; //categoryAxis.renderer.minGridDistance = 30; var valueAxis = chart.yAxes.push(new am4charts.ValueAxis()); // Create series var series = chart.series.push(new am4charts.ColumnSeries()); series.dataFields.valueY = "value"; series.dataFields.categoryX = "category"; var indicator; var indicatorInterval; function showIndicator() { if (!indicator) { indicator = chart.tooltipContainer.createChild(am4core.Container); indicator.background.fill = am4core.color("#fff"); indicator.width = am4core.percent(100); indicator.height = am4core.percent(100); var indicatorLabel = indicator.createChild(am4core.Label); indicatorLabel.text = "Loading stuff..."; indicatorLabel.align = "center"; indicatorLabel.valign = "middle"; indicatorLabel.dy = 50; var hourglass = indicator.createChild(am4core.Image); hourglass.href = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-160/hourglass.svg"; hourglass.align = "center"; hourglass.valign = "middle"; hourglass.horizontalCenter = "middle"; hourglass.verticalCenter = "middle"; hourglass.scale = 0.7; } indicator.hide(0); indicator.show(); clearInterval(indicatorInterval); indicatorInterval = setInterval(function() { hourglass.animate([{ from: 0, to: 360, property: "rotation" }], 2000); }, 3000); } function hideIndicator() { indicator.hide(); clearInterval(indicatorInterval); } showIndicator(); 的变量,只需有一个事件处理程序即可在单击<script src="//www.amcharts.com/lib/4/core.js"></script> <script src="//www.amcharts.com/lib/4/charts.js"></script> <script src="//www.amcharts.com/lib/4/themes/animated.js"></script> <div id="chartdiv"></div> 时从textarea读取值。这样的好处是避免了不必要的全局变量。试试这个:

textarea
a

这里要注意的一件事是,您必须注意该值中的换行符。

或者,如果您确实想在输入<textarea id="post" type="text"></textarea> <a href="#" class="id">Reload</a> 时更新$(document).ready(function() { $('#reload').click(function() { location.assign('/arany/?i=' + $('#post').val()); }); }); 的{​​{1}},则可以使用href在{{1 }}事件处理程序:

a
textarea

答案 3 :(得分:0)

我看到您想在单击"i"按钮时将textarea内容作为查询字符串"Reload"参数添加。

为此,您只需要输入文本字段而不是文本区域,因为URL不支持换行符。

此外,您不需要每次更改文本时都更新"i",只需单击"Reaload"按钮,您就需要该值。

所以,这是我建议您解决的问题:

<input type="text" id="post"></textarea>
<a onclick="goToLocation();">Reload</a>

function goToLocation(){
    apry = window.document.getElementById('post').text();
    window.location.href = 'http://localhost/arany/?i=' + apry;
}

答案 4 :(得分:-1)

Auto_Part

这应该为您工作