简体   繁体   English

将脚本标签值传递给html中的输入标签

[英]Pass script tag value to input tag in html

I am trying to pass a particular variable value from the script tag to an input tag. 我试图将特定的变量值从脚本标签传递到输入标签。 But somehow it is not working. 但是它不起作用。

I am trying to pass variable1 value from the below code from script tag to input tag. 我正在尝试将以下代码中的variable1值从脚本标签传递到输入标签。

So suppose variable1 value is John then this line in my code will look like this- 因此,假设variable1值为John那么我代码中的这一行将如下所示:

<input ONCLICK="window.location.href='some_url&textId=John'">

Below is the code 下面是代码

<html>
<head>
    <title>Applying</title>
</head>
<body>

<script>
function getUrlVars() {

    // some code

}
var variable1 = getUrlVars()["parameter1"];
var variable1 = unescape(variable1);

// some more code

</script>

<input ONCLICK="window.location.href='some_url&textId=variable1'">

</body>
</html>

Can anyone explain me what wrong I am doing? 谁能解释我在做什么错?

Try it that way: 尝试这种方式:

var variable1 = getUrlVars()["parameter1"];
variable1 = unescape(variable1);
document.getElementById('Apply').onclick = function() {
    window.location.href = 'some_url&textID=' + variable1;
};

That attaches a function to the onclick event that exactly does what you want. 它将一个函数附加到onclick事件上,该函数正是您想要的。 For the initial input element simply remove the onclick attribute: 对于初始输入元素,只需删除onclick属性:

<input name="Apply" type="button" id="Apply" value="Apply" />

I would change it to the following if your trying to bind the click handler to this input element: 如果您尝试将点击处理程序绑定到此输入元素,则将其更改为以下内容:

<html>
<head>
    <title>Applying</title>
</head>
<body>

<script>
function getUrlVars() {

    // some code

}
var variable1 = getUrlVars()["parameter1"];
var variable1 = unescape(variable1);

document.getElementById("Apply").onclick = function() {
    window.location.href='some_url&textId=' + variable1;
}

// some more code

</script>

<input name="Apply" type="button" id="Apply" value="Apply" >

</body>
</html>

I haven't tested it yet but it should work. 我还没有测试过,但是应该可以。

If you wish to perform inline functions, you need to wrap the code in an executable closure: 如果要执行内联函数,则需要将代码包装在可执行的闭包中:

<input name="Apply" type="button" id="Apply" value="Apply" ONCLICK="(function() {window.location.href='your_data'})();">

As this can be largely unmaintainable, I recommend you abstract this functionality into a more organized place in your application. 由于这在很大程度上难以维护,因此我建议您将此功能抽象到应用程序中更有条理的位置。

(function(window, $, undefined) {
  // assuming you use jQuery
  $('#Apply').click(function() {
   window.location.href = '';// your code
  })
})(window, $);

I may be totally misunderstanding what you want to do, but I hope this helps. 我可能完全误解了您想做什么,但是我希望这会有所帮助。

The whole url parameters bit is surely unnecessary. 整个网址参数位肯定是不必要的。

You can just set the value attribute in the field: 您可以在字段中设置value属性:

var field = document.getElementById('textfield');
var value = 'Some text';

field.addEventListener("click", function () {
    this.setAttribute('value', value);
});

Here's a jsfiddle example: http://jsfiddle.net/LMpb2/ 这是一个jsfiddle示例: http : //jsfiddle.net/LMpb2/

You have it inside the ' ' you need to add it into the string. 您将其包含在“”中,需要将其添加到字符串中。 So try 所以尝试

         "window.location.href='some_url&textId='+variable1+';'"

at onclick call a function, inside that function set window.locatio.href ! 在onclick处调用一个函数,在该函数集内window.locatio.href!

a sample 一个样品

<script>
var url="www.google.com";
function myfunc(){
alert(url);
}
</script>

<input type="button" onclick="myfunc()" value="btn" >

http://jsfiddle.net/CgKHN/ http://jsfiddle.net/CgKHN/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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